Forum Moderators: coopster

Message Too Old, No Replies

Deleting unwanted characters from a string

         

stormshield

3:16 pm on May 5, 2009 (gmt 0)

10+ Year Member



How can I filter a string so that only characters [a-zA-z] are left?

E.g.: "I'm354asdk[](t)" ->Imasdkt

whoisgregg

3:21 pm on May 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In regular expressions, using the caret "^" at the beginning of a character block means to match everything except the characters listed:

$clean = preg_replace('/[^a-zA-Z]+/', '', $string);

stormshield

3:22 pm on May 5, 2009 (gmt 0)

10+ Year Member



I will test it today.

Thanks!

rocknbil

5:28 pm on May 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



/[^a-zA-Z]+/

This will also work:

'/[^A-Z]+/i' (same thing, case-insensitive modifer)

Careful how you use this, Note from Dr. Doc: [webmasterworld.com]

"Sorry, your name must only contain letters (A-Z)" error message. Let me assure you that my full legal name contains nothing but letters. Let me also assure you that A-Z are not the only letters out there. Even worse -- occasionally the "non-letter" is just silently removed.