How to remove none-characters and digits from a string?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

How to remove none-characters and digits from a string?

Post by jasongr »

Hi people

I have a string that could contain any character (from any language).
I would like to remove all non-characters and digits from it.
I could use:

Code: Select all

$new_string = ereg_replace("[^A-Za-z0-9]", "", $string);
however this will only work for strings with English characters.
I am looking for a solution that will also work for example in German, Chinese and so on.

Does anyone know of such a solution?

regards
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

the \w set may include non-roman characters, but I'd double check to be sure
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post by jasongr »

I found out that mb_ereg_replace and blacklisting unwanted letters is the way to go
Post Reply