I want to convert all html tags(  > < etc) to text format; I have try
html_entity_decode()
but it will return ? if  .
Use htmlspecialchars_decode
is the opposite of htmlspecialchars
.
Example from the PHP documentation page:
$str = '<p>this -> "</p>';
echo htmlspecialchars_decode($str);
//Output: <p>this -> "</p>
Note that
does not denote a space character, but rather a non-breaking space sequence. To decode it as well, use html_entity_decode()
.
, please see link for details stackoverflow.com/questions/11737675/…
Commented
Jul 7, 2021 at 0:43
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 "walk" the <b>dog</b> now
echo $b; // I'll "walk" the <b>dog</b> now
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 XYZ') );
?>
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
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
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(" ", "", $str);
$str = preg_replace("/\s+/", " ", $str);
$str = trim($str);
return $str;
}
$html = " Hello world! lorem ipsum.";
$output = clean($html);
echo $output;
Hello world! lorem ipsum.