Regulgar Expression syntax help...

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


I could do some basic regular expressions (which I picked up from sed,  
in unix) but I'm afraid its been a while.  I'm wondering if someone  
could help out.  I basically want to check that a string variable  
contains only alphanumeric characters, in addition check for the  
underscore.  No decimal point check is required, and it should be a case  
insensitive check.

I'm using PHP 4.x and would basically like to replace whatever is not  
named above with an empty character (thus if a bang or question mark was  
  in a string, it would be removed from the string).

I know I could use  something like preg_replace, but I don't know how to  
say "when not found"... In sed, I could do something like

sed -e -i 's/[a-z]|[0-9]|_//g'

This would do the opposite of what I want to do (I think) but I'm lost  
when it  comes to PHP...

Can someone help me out, via the newsgroup (so all can learn?)

thanks in advance,
randell d.

Re: Regulgar Expression syntax help...

Quoted text here. Click to load it

preg_replace("#[^a-z0-9_]#", "", $subject);

Met vriendelijke groeten,
Tim Van Wassenhove <

Re: Regulgar Expression syntax help...

Tim Van Wassenhove wrote:

Quoted text here. Click to load it

and #2 of the 100 ways to write that expression - probably  
*the* most common in c.l.php - involves + optimi[sz]ation  
(likely not appreciable though):

 #[^a-z0-9_]+#i  <- case-insensitive, mind

in at no. 3, the less explicit:

 #[^[:alnum:]_]+#  //no need for 'i'; can match non-US-ASCIIs

down at number 4, the shorter and even more implicit:

 #\W+#  //as with 2, can match non-US-ASCIIs, no need for 'i'

they're all different in their own inimitable ways.


Re: Regulgar Expression syntax help...

John Dunlop wrote:
Quoted text here. Click to load it

Thanks to you both for the suggestions...

My internet access is limited at the moment but I will test this in a  
few hours (when I have my LAMP machine)...

However... I'm curious, the solutions provided, when used with  
preg_replace, would replace any of those characters, as opposed to  
replace anything BUT those characters... (ie I want to replace anything  
that is NOT alphanumeric or an underscore with a nospace...)

Thanks again for the help,
Randell D.

Re: Regulgar Expression syntax help...

Randell D. wrote:

Quoted text here. Click to load it

That's what they do, for differing values of 'alpha'.  The ^  
negates the character class, so [^a-z] matches everything  
except a, b, c, ... z.  [[:alnum:]] in addition to the decimal  
digits can cover other letters.  \W matches any non-word  
character (word = letters, numbers and underscore, where  
'letters' depends on your locale settings).


Site Timeline