Search code examples
phpstringstr-replacehtmlspecialchars

HTML Special Characters & Removing Semi Colon


So say I had the below varriable

$str = Brand Name;™ SubBrand Name

And I wanted it to display as Brand Name™ SubBrand Name

So basically I'm trying to figure out a way to decode html entities (Any of them) and remove extra semi colons / colons from the string.

$str = 'Brand Name;™ SubBrand Name';
$str = htmlspecialchars_decode($str);
$str = str_replace(';', '', $str);
echo $str;

Assuming this is how my string is going to look, what is the best way to go about this?


Solution

  • You almost have it. Use html_entity_decode() instead, replace, then htmlentities() to encode again:

    $str = 'Brand Name;™ SubBrand Name';
    $str = html_entity_decode($str);
    $str = str_replace(';', '', $str);
    $str = htmlentities($str);
    echo $str;