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
Removing everything but numbers & letters...
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Moved to Regex.
Alternatively, if you do not wish to use regex, you could just a bunch of str_replace()
Alternatively, if you do not wish to use regex, you could just a bunch of str_replace()
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:
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Code: Select all
$string = 'some text here 123 ***###@@;;;,,,&&^%%% stuff Exclaim!!!';
$newstring = preg_replace('/[^a-z\d\s]+/s', '', $string);
//some text here 123 stuff ExclaimProblem with \W+ as McGruff says is that it leaves underscores