Forum Moderators: coopster

Message Too Old, No Replies

preg replace with extra characters

         

username

5:38 am on Feb 22, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi, I have been using a preg_replace to remove all non alphanumeric characters from strings for a while now, but the need to undertake a preg_replace whereby all chracters that are non standard characters i.e numbers, letters, and punctuation (full stops, dashes, question marks etc) needs to be implemented.

I use to remove non alphanumeric characters:

$str = preg_replace("/[^a-zA-Z0-9s]/", "", $str);

Is there a way to append some of the allowed characters into the function, or some other preg_replace method of doing this? It does need to be a preg_replace for other reasons, otherwise I would have simply used a str_replace multiple times.

Thanks.

rob7591

5:55 am on Feb 22, 2009 (gmt 0)

10+ Year Member



This pattern should work:

"/[^\x20-\x7E]/i"

username

11:32 pm on Feb 22, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



No, what I really need is something that just approves alphanumeric characters, and full stops (.), and dashes (-). Is there a way to append those two characters to my preg_replace?

rob7591

1:35 am on Feb 23, 2009 (gmt 0)

10+ Year Member



The above pattern takes all characters on the English keyboard. To accept only alphanumeric, ., and -, use:

"/[^a-z0-9\.\-]/i"