How to remove html special chars?

Asked : Nov 17

Viewed : 63 times

I want to extract only text from a PHP string.

I want to remove HTML tags, which is done by strip_tags

  & © 

Please tell me any function which I can use to remove these special code chars from my string.

php html-encode 

Nov 17

1 Answers

Either decode them using html_entity_decode or remove them using preg_replace:

Method 1 :  html_entity_decode();

$orig = "I'll \"walk\" the <b>dog</b> now";

$a = htmlentities($orig);
$b = html_entity_decode($a);

echo $a; // I'll &quot;walk&quot; the &lt;b&gt;dog&lt;/b&gt; now
echo $b; // I'll "walk" the <b>dog</b> now

Method 2 :  preg_replace();

$Content = preg_replace("/&#?[a-z0-9]+;/i","",$Content); 

might be nice to replace the '+' with {2,8} or something. This will limit the chance of replacing entire sentences when an unencoded '&' is present.

$Content = preg_replace("/&#?[a-z0-9]{2,8};/i","",$Content); 

answered Nov 26

Login and Submit Your Answer
Browse other questions tagged  php  html-encode or ask your own question.