PHP remove all special characters from string

1.68K views
no comments
1 Oct 2013 2:25 am

Its always needs developers to break the special characters from user data like name or url slug or another things, so some times users wants add user name with special characters, but we don’t allow them to add special characters in their name, so here code you can use it to remove special characters, and see result of this function below.

function remove_special_chrs ($string="") {

 // PREG_REPLACE REMOVE ALL OTHER CHARACTERS THAT NOT AVAIALABLE IN PREG_REPLACE FIRST
 // PARAMETER YOU CANNOT UNDERSTAND FIRST PARAMETER YOU MUST READ PHP REGULAR EXPRESSION!
 $string = preg_replace('/[^A-Za-z0-9]/','',$string);

 //STRIP_TAGS REMOVE HTML TAGS
 $string=strip_tags($string,"");

 //HERE WE REMOVE WHITE SPACES AND RETURN IT
 return trim($string);
}

echo remove_special_chrs('/**afdas+/0011-.▬1r○Xù2G·NΘí ANOTHER CHARACTERS ℗™⅓⅖↘◊ẃћѝѵѓѲѣҐẂћіђґẅỲєѳ⁵†Ѵẁ⁼');
// CALLED THIS FUNCTION ITS ECHO THE "afdas00111rX2GNANOTHERCHARACTERS"

RESULT

afdas00111rX2GNANOTHERCHARACTERS

NOTE:Your Email Address will be not shown and please do not add spamming comments because here is REL="NOFOLLOW" on your links and comments also moderated shown.
<code>Put html css or any language code under this tag</code>