Removing everything but numbers & letters...

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

Moderator: General Moderators

Post Reply
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Removing everything but numbers & letters...

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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 ;)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Moved to Regex.

Alternatively, if you do not wish to use regex, you could just a bunch of str_replace()
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

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

Post 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 ;)
Post Reply