My data has many HTML entities in it (•
...etc) including '
. I just want to convert it to its character equivalent.
I assumed htmlspecialchars_decode() would work, but - no luck. Thoughts?
I tried this:
echo htmlspecialchars_decode('They're here.');
But it returns: They're here.
Edit:
I've also tried html_entity_decode(), but it doesn't seem to work:
echo html_entity_decode('They're here.')
also returns: They're here.
Since
'
is not part of HTML 4.01, it's not converted to'
by default.In PHP 5.4.0, extra flags were introduced to handle different languages, each of which includes
'
as an entity.This means you can do something like this:
You will need both
ENT_QUOTES
(convert single and double quotes) andENT_HTML5
(or any language flag other thanENT_HTML401
, so choose the most appropriate to your situation).Prior to PHP 5.4.0, you'll need to use str_replace:
This should work:
The
'
entity and a lot of others are not in the PHP translation table used byhtml_entity_decode
andhtmlspecialchars_decode
functions, unfortunately.Check this comment from the PHP manual: http://php.net/manual/en/function.get-html-translation-table.php#73410
There is a "right" way, without using
str_replace
, @cbuckley was right it's because the default forhtml_entity_decode
is HTML 4.01, but you can set an HTML 5 parameter that will decode it.Use it like this:
What you are actually looking for is
html_entity_decode()
.html_entity_decode()
translates all entities to characters, whilehtmlspecialchars_decode()
only reverses whathtmlspecialchars()
will encode.EDIT: Looking at the examples on the page I linked to, I did a bit more investigation and the following seems to not work:
This is why it's not working. Why it's not in the lookup table is another question entirely, something I can't answer unfortunately.
Have you tried using
echo htmlspecialchars('They're here.')
?I think that is what you are looking for.