PHP convert html to sample, & gt; to> etc.

I want to convert all html tags (& nbsp & gt & lt, etc.) to text format; I tried

html_entity_decode() 

but will he come back? if & nbsp.

+8
html php
source share
5 answers

Using htmlspecialchars_decode is the opposite of htmlspecialchars .
An example on the PHP documentation page:

  $str = '<p>this -&gt; &quot;</p>'; echo htmlspecialchars_decode($str); //Output: <p>this -> "</p> 
+14
source share

html_entity_decode () is the opposite of htmlentities () in that it converts all HTML objects to string to their applicable characters.

 $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 
+8
source share

Use

  html_entity_decode () 
instead
  html_entity_encode () 
+3
source share

If you check the html_entity_decode () manual:

You may wonder why trim (html_entity_decode ('')); do not reduce the string to an empty string, because the `` object is not ASCII 32 code (which is split using trim ()), but ASCII code 160 (0xa0) in the standard ISO 8859-1 character set.

You can insert the html_entity_decode () function inside str_replace () in ASCII # 160 into space:

 <?php echo str_replace("\xA0", ' ', html_entity_decode('ABC &nbsp; XYZ') ); ?> 
+2
source share

I know that my answer comes very late, but I thought it could help someone else. I believe the best way to extract all special characters is to use utf8_decode () in php. Even for working with &nbsp; or any other special character using space, use utf8_decode() .

After using utf8_decode() you can manipulate these characters directly in the code. For example, in the following code, the clean () function replaces &nbsp; empty. Then it replaces all the extra spaces with a single space using preg_replace() . Leading and trailing spaces are removed using trim() .

 function clean($str) { $str = utf8_decode($str); $str = str_replace("&nbsp;", "", $str); $str = preg_replace("/\s+/", " ", $str); $str = trim($str); return $str; } $html = "&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;Hello world! lorem ipsum."; $output = clean($html); echo $output; 

Hello World! lorem ipsum.

+1
source share

All Articles