tags:

views:

4531

answers:

7

I need to match a space character in php regex. Anyone got any ideas?

EDIT

I mean like "gavin schulz" the space in between the two words. I'll make another clarification. I did try and find more info but nothing turned up. Anyways I am using a regex to make sure that I only allow letters, number and a space. But I'm not sure how to find the space. This is what I have right now:

$newtag = preg_replace("/[^a-zA-Z0-9s|]/", "", $tag);

A: 

In Perl the switch is \s (whiteSpace). FWIW

John Spooner
Nope - \w is "word character". You're thinking of \s.
Paul Beckingham
*oops* that's right...<buries head in sand>...thanks
John Spooner
Uh, if you came back to comment, why didn't you fix your answer?
Cebjyre
Good point. Let me take care of that for you....
paxdiablo
A: 

You can also use the \b for a word boundary. For the name I would use something like this:

[^\b]+\b[^\b]+(\b|$)

EDIT Modifying this to be a regex in Perl example

if( $fullname =~ /([^\b]+)\b[^\b]+([^\b]+)(\b|$)/ ) {
 $first_name = $1;
 $last_name = $2;
}

EDIT AGAIN Based on what you want:

$new_tag = preg_replace("/[\s\t]/","",$tag);
Suroot
+2  A: 

It seems to me like using a REGEX in this case would just be overkill. Why not just just strpos to find the space character. Also, there's nothing special about the space character in regular expressions, you should be able to search for it the same as you would search for any other character. That is, unless you disabled pattern whitespace, which would hardly be necessary in this case.

Kibbee
A: 

\040 matches exactly the space character.

http://us.php.net/manual/en/regexp.reference.php

davethegr8
+2  A: 

You might also try one of the online PHP regular expression testers, such as:

http://www.spaweditor.com/scripts/regex/

or

http://www.regextester.com/

runako
Thanks for linking to these sites, they are great!
Gavin Schulz
+10  A: 

If you're looking for a space, that would be " " (one space).

If you're looking for one or more, it's " *" (two spaces and an asterisk) or " +" (one space and a plus).

If you're looking for common spacing, use "[ X]" or "[ X][ X]*" or "[ X]+" where X is the physical tab character (and is preceded by spaces in all those examples).

These will work in every RE engine I've ever seen (some of which don't even have the one-or-more "+" character, ugh).

If you know you'll be using one of the more modern RE engines, "\s" and its variations are the way to go. In addition, I believe word boundaries match start and end of lines as well, important when you're looking for words that may appear without preceding or following spaces.

For PHP specifically, this page may help.

From your edit, it appears you want to remove all non valid characters The start of this is (note the space inside the RE):

$newtag = preg_replace ("/[^a-zA-Z0-9 ]/", "", $tag);

If you also want trickery to ensure there's only one space between each word and none at the start or end, that's a little more complicated (and probably another question) but the basic idea would be:

$newtag = preg_replace ("/ +/", " ", $tag);
$newtag = preg_replace ("/^ +/", "", $tag);
$newtag = preg_replace ("/ +$/", "", $tag);
paxdiablo
Not sure who down-voted your answer. Seems completely valid (and correct) to me. +1.
strager
Thanks, strager, I suspect it was -1'ed before I put the PHP-specific stuff in, althoughI would have still considered it helpful. But who am I to dictate what other people consider useful or not? Such are the slings and arrows I'm forced to endure :-)
paxdiablo
His original regex seemed to want to replace the " " character. You are negating the space, therefore his space won't be "deleted" as intended.
Suroot
Quoting: "only allow letters, number and a space", Gavin's original RE was wrong (which is why he was asking the question). My RE deletes everything that isn't one of those.
paxdiablo
Ah understood, my fault for misunderstanding; fyi, I did not vote you down.
Suroot
That's okay Suroot, I don't engage in retaliation unless someone's being truly vindictive, nitpickey or pig-headed :-) - you were none of those.
paxdiablo
+1 from me for the \s.
RichardOD
A: 

I am using a regex to make sure that I only allow letters, number and a space

Then it is as simple as adding a space to what you've already got:

$newtag = preg_replace("/[^a-zA-Z0-9 ]/", "", $tag);

(note, I removed the s| which seemed unintentional? Certainly the s was redundant; you can restore the | if you need it)

If you specifically want *a* space, as in only a single one, you will need a more complex expression than this, and might want to consider a separate non-regex piece of logic.

Peter Boughton