Page 1 of 1

Removing everything but numbers & letters...

Posted: Sat May 07, 2005 6:03 am
by Mr Tech
How do I remove EVERYTHING in a peice of text excpet numebrs & letters? This would mean ti worul remove any characters such as

! @ # $ % ^ ' , . etc etc etc

Thanks

Posted: Sat May 07, 2005 6:58 am
by timvw
i would use preg_replace everything that is not [a-zA-Z0-9].

now it's up to you to lookup that function, and your regular expressions book how to write that ;)

Posted: Sat May 07, 2005 8:32 am
by John Cartwright
Moved to Regex.

Alternatively, if you do not wish to use regex, you could just a bunch of str_replace()

Posted: Sat May 07, 2005 11:33 am
by McGruff
Another similar way to define the pattern would be: \W+

In English, that means: "one or more of anything which is not a word char" (\w). From the php manual:
A "word" character is any letter or digit or the underscore character, that is, any character which can be part of a Perl "word". The definition of letters and digits is controlled by PCRE's character tables, and may vary if locale-specific matching is taking place (see "Locale support" above). For example, in the "fr" (French) locale, some char- acter codes greater than 128 are used for accented letters, and these are matched by \w.

Posted: Sat May 07, 2005 6:04 pm
by Chris Corbyn

Code: Select all

$string = 'some text here 123 ***###@@;;;,,,&&^%%% stuff Exclaim!!!';
$newstring = preg_replace('/[^a-z\d\s]+/s', '', $string);
//some text here 123  stuff Exclaim
Should do it. Remove the \s to also strip out white-space from the string.

Problem with \W+ as McGruff says is that it leaves underscores ;)