Page 1 of 1

\W+ with ;

Posted: Fri Mar 31, 2006 2:26 am
by shiznatix
would this:

Code: Select all

$input = preg_replace('/\W+ \;/', '', $input);
replace everything that is NOT alphanumeric, a space, a underscore, or a semi-colon? I don't want this to do anything crazy that I am not aware of.

Posted: Fri Mar 31, 2006 4:53 am
by Chris Corbyn
Nope... that would replace anything NOT alphanumeric, FOLLOWED BY a space, FOLLOWED BY a semi-colon ;)

Use a character class to give groups of characters to replace ;) [abc]

Code: Select all

/[\W ;]+/

Posted: Fri Mar 31, 2006 9:39 am
by feyd
For your information, \W doesn't really do what you want either.

Code: Select all

[feyd@home]>php -r "$a = ''; for($i = 0; $i < 256; $i++, $a .= chr($i)); echo preg_replace('#\W#','',$a);"
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyzâèîÄÜ£&#8359;ƒ¬&#9619;&#9474;&#9569;&#9571;&#9553;&#9492;&#9524;&#9516;&#9500;&#9472;&#9532;&#9566;&#9567;&#9562;&#9556;&#9577;&#9574;&#9568;&#9552;&#9580;&#9575;&#9576;&#9572;&#9573;&#9561;&#9560;&#9554;&#9555;&#9578;&#9496;&#9484;&#9608;&#9604;&#9612;&#9616;&#9600;&#945;ß&#915;&#960;&#931;&#963;µ&#964;&#934;&#920;&#937;&#948;&#8734;&#966;&#949;&#8745;&#8801;±&#8805;&#8804;&#8992;&#8993;÷°&#8729;·&#8730;&#8319;²&#9632;

Posted: Sat Apr 01, 2006 9:17 am
by shiznatix

Code: Select all

/[\W ;] /
this does not work. it does not allow the ; character to pass through

Posted: Tue Apr 18, 2006 5:24 am
by shiznatix
kinda a bump since i was not able to figure it out but instead of the ; character I need to let the - character through. this does not work

Code: Select all

/[\W -]+/

Posted: Tue Apr 18, 2006 7:55 am
by feyd

Code: Select all

#[^a-zA-Z0-9_ -]+#
That's for preg_replace()

Posted: Tue Apr 18, 2006 8:13 am
by shiznatix
thanks mate. beautiful as always