PHP: htmlspecialchars, but keep HTML entities

798 views Asked by At

I've been working on a small side project which formats Skype logs to make them look nice. This is going great, but I have hit a somewhat annoying bump in the road.

Here is a great (and somewhat obscene) example of what is going on. As you can see, htmlspecialchars() also converts HTML character codes into a string (because & becomes &). I was wondering if there was a way to allow HTML entities to still remain through htmlspecialchars?

2

There are 2 answers

0
Bruce On

Use this function:

$string=$_POST[pname];
function seoUrl($string) {
    //Lower case everything
    $string = strtolower($string);
    $string = str_replace('&',' ',$string);
    $string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
    $string = preg_replace("/[\s-]+/", " ", $string);
    $string = preg_replace("/[\s_]/", "-", $string);
    return $string;
}
$sclean=seoUrl($string);

You can use str_replace('&',' ',$string); instead of & use what character you want remove...

0
Sarvagna Mehta On

You can use htmlentities() if it solve your problems, else store the string in one variable which returns from the functions and then while printing that string

$foo = str_replace("&","&","$bar");