19

I want to convert all html tags(&nbsp &gt &lt etc) to text format; I have try

html_entity_decode() 

but it will return ? if &nbsp.

1
  • 1
    Why don't you test? And also scroll down on the manual page because there are related functions listed.
    – hakre
    Commented Mar 8, 2013 at 8:55

5 Answers 5

22

Use htmlspecialchars_decode is the opposite of htmlspecialchars.
Example from the PHP documentation page:

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

Note that &nbsp; does not denote a space character, but rather a non-breaking space sequence. To decode it as well, use html_entity_decode().

3
  • 1
    @rikiless, RavatSinh's solution worked for me in removing all HTML tags, including &nbsp;.
    – Pamela
    Commented Aug 22, 2014 at 22:15
  • 4
    this solution does not help with &nbsp;, please see link for details stackoverflow.com/questions/11737675/… Commented Jul 7, 2021 at 0:43
  • 4
    Use html_entity_decode() instead since htmlspecialchars_decode decodes only some characters. e.g. &nbsp; stays unchanged. Commented Nov 19, 2021 at 9:56
18

html_entity_decode() is the opposite of htmlentities() in that it converts all HTML entities in the 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
7

Use

 html_entity_decode() 
instead of
 html_entity_encode() 

5

If you check the html_entity_decode() manual:

You might wonder why trim(html_entity_decode(' ')); doesn't reduce the string to an empty string, that's because the ' ' entity is not ASCII code 32 (which is stripped by trim()) but ASCII code 160 (0xa0) in the default ISO 8859-1 characterset.

You can nest your html_entity_decode() function inside a str_replace() to ASCII #160 to a space:

<?php

echo str_replace("\xA0", ' ', html_entity_decode('ABC &nbsp; XYZ') );

?>
1

I know my answer is coming in really late but thought it might help someone else. I find that the best way to extract all special characters is to use utf8_decode() in php. Even for dealing with &nbsp; or any other special character representing blank space use utf8_decode().

After using utf8_decode() one can manipulate these characters directly in the code. For example, in the following code, the function clean() replaces &nbsp; with a blank. Then it replaces all extra white spaces with a single white space using preg_replace(). Leading and trailing white 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.