Page 1 of 1

All non alphanumeric but also allow _

Posted: Wed Mar 08, 2006 10:54 am
by shiznatix
Just as the title states, I need to strip out everything that is not alphanumeric but I need to allow the _ character through. How do I do this?

Posted: Wed Mar 08, 2006 11:06 am
by feyd

Code: Select all

$data = preg_replace('#[^a-z_]+#i', '', $data);

Posted: Wed Mar 08, 2006 11:06 am
by Chris Corbyn
\W (Uppercase) ?

Code: Select all

preg_replace('/\W+/', '', $string);

Posted: Wed Mar 08, 2006 11:25 am
by shiznatix
ahha and life is now easy

Posted: Wed Mar 08, 2006 11:34 am
by shiznatix
d11wtq's worked the charm. feyd sorry but yours works perfect unless it is a single digit that goes it, if it is just a single digit it turns it to null.

Posted: Wed Mar 08, 2006 11:47 am
by feyd
oops, I forgot to add numbers. :P

Code: Select all

$data = preg_replace('#[^a-z0-9_]+#', '', $data);

Posted: Wed Mar 08, 2006 2:33 pm
by shiznatix
silly silly. How would I edit that to allow a blank space though as well?

Posted: Wed Mar 08, 2006 2:42 pm
by R4000
To allow space use: (I THINK! don't bite me if i'm wrong)

Code: Select all

$data = preg_replace('#[^a-z0-9 _]+#', '', $data);
Just basicly add anything you want inbetween the [ and ]

If you wanted to allow the ; char. :

Code: Select all

$data = preg_replace('#[^a-z0-9;_]+#', '', $data);

Posted: Wed Mar 08, 2006 2:51 pm
by shiznatix
thanks mate ill give that a shot

Posted: Wed Mar 08, 2006 3:28 pm
by feyd
my brain must be off today, I forgot the i modifier for the pattern in the updated one. :roll:

Code: Select all

$data = preg_replace('#[^a-z0-9 _]+#i', '', $data);
with space

Posted: Wed Mar 08, 2006 6:01 pm
by Chris Corbyn
feyd wrote:my brain must be off today, I forgot the i modifier for the pattern in the updated one. :roll:

Code: Select all

$data = preg_replace('#[^a-z0-9 _]+#i', '', $data);
with space
Or even easier:

#[^\w ]+#

:P

No "i" needed :lol: I never get why people use a-z0-9_ when that's precisely what \w is :idea:

Posted: Wed Mar 08, 2006 6:32 pm
by feyd
d11wtq wrote:
feyd wrote:my brain must be off today, I forgot the i modifier for the pattern in the updated one. :roll:

Code: Select all

$data = preg_replace('#[^a-z0-9 _]+#i', '', $data);
with space
Or even easier:

#[^\w ]+#

:P

No "i" needed :lol: I never get why people use a-z0-9_ when that's precisely what \w is :idea:
You're mistaken.. ;)

Code: Select all

<?php

$str = '';

for($i = 0; $i < 256; $i++)
{
	$str .= chr($i);
}

echo preg_replace('#[^\w]#', '', $str);

?>

Code: Select all

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;