\W+ with ;

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

\W+ with ;

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;]+/
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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;
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Code: Select all

/[\W ;] /
this does not work. it does not allow the ; character to pass through
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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 -]+/
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

#[^a-zA-Z0-9_ -]+#
That's for preg_replace()
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

thanks mate. beautiful as always
Post Reply