Forum Moderators: coopster

Message Too Old, No Replies

Removing special characters from a string using php?

Special characters removing with PHP

         

anilguha

11:02 pm on Jul 28, 2007 (gmt 0)

10+ Year Member



Hello,

I have string with special characters like "$%#?,{}*@". I want to remove all these characters , but only if, they precede or is after the string.
Eg. Hello?, the question mark after Hello, I need to remove, but say catch?is, here question mark is ok.

Thanks in advance,
Rupak Banerjee.

RonPK

9:10 am on Jul 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$clean = preg_replace("/^[^a-z0-9]?(.*?)[^a-z0-9]?$/i", "$1", $text);

This might remove any character that
* is not a-z
* is not a digit
* is the first or the last character in the string