I need a function that will clean a strings' special characters. I do NOT want this to convert HTML characters like
<br />
to
<br />
I want to convert things like: •, ½, ’ to html code.
This is the function I currently use, but it doesn't appear to work with the fractions..
function cleanText($str){
$str = str_replace("Ñ" ,"Ñ", $str);
$str = str_replace("ñ" ,"ñ", $str);
$str = str_replace("ñ" ,"ñ", $str);
$str = str_replace("Á","Á", $str);
$str = str_replace("á","á", $str);
$str = str_replace("É","É", $str);
$str = str_replace("é","é", $str);
$str = str_replace("ú","ú", $str);
$str = str_replace("ù","ù", $str);
$str = str_replace("Í","Í", $str);
$str = str_replace("í","í", $str);
$str = str_replace("Ó","Ó", $str);
$str = str_replace("ó","ó", $str);
$str = str_replace("“","“", $str);
$str = str_replace("”","”", $str);
$str = str_replace("‘","‘", $str);
$str = str_replace("’","’", $str);
$str = str_replace("—","—", $str);
$str = str_replace("–","–", $str);
$str = str_replace("™","™", $str);
$str = str_replace("ü","ü", $str);
$str = str_replace("Ü","Ü", $str);
$str = str_replace("Ê","Ê", $str);
$str = str_replace("ê","î", $str);
$str = str_replace("Ç","Ç", $str);
$str = str_replace("ç","ç", $str);
$str = str_replace("È","È", $str);
$str = str_replace("è","è", $str);
$str = str_replace("•","•" , $str);
$str = str_replace("¼","¼" , $str);
$str = str_replace("½","½" , $str);
$str = str_replace("¾","¾" , $str);
$str = str_replace("½","½" , $str);
return $str;
}
You can replace your entire function with htmlentities using the ENT_SUBSTITUTE attribute. It will perform much faster in addition to working correctly.
Note: ENT_SUBSTITUTE available as of PHP 5.4.
Guess it's time to take a look at the htmlentities PHP function, and its options.
Basically, you can replace your whole function with:
$str = htmlentities( $str );
It will be also a lot more efficient.
Be sure to take a look at the function's optional parameters, if you need special processing (especially ENT_SUBSTITUTE).
$str = htmlentities( $str, ENT_SUBSTITUTE );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With