I have a problem with decoding html entities for browser display.
I’m trying to display the blog description from a wordpress blog. The text is being html entity encoded before it is saved in the db. So in order to display, for instance, a hyper link the text has to be html entity decoded back, so the a-tag is being rendered properly.
But when I try to decode the text it still comes out as html entities.
The output before being decoded:
echo(bloginfo( 'description' )); //output: Display a hyper link. <a href="">READ MORE</a>
The output when being decoded. And here’s my problem. It is still not decoded! Check the output.
echo(html_entity_decode(bloginfo( 'description' ))); //output: Display a hyper link. <a href="">READ MORE</a>
And when I try to hard code the text to be decoded, it works!
echo(html_entity_decode('Display a hyper link. <a href="">READ MORE</a>')); //output: Display a hyper link. <a href="">READ MORE</a>
I’ve looked at the php manual, and tried different charsets and quote styles as arguments. But still no luck.
What am I doing wrong, any ideas?
The bloginfo function does not return the text, it echoes it directly. Your “echo” will therefore in fact echo nothing!
Use get_bloginfo instead.
You should read the WordPress manual 🙂
It clearly states it prints the results to the browser, use get_bloginfo instead.
http://codex.wordpress.org/Function_Reference/bloginfo
For clarity, it doesnt work because bloginfo echo’s the information, it doesnt return it at all. You could have tested this by checking (by using var_dump) the return value of bloginfo.
Are you sure
bloginfo
does not encode the characters another time?If it re-encodes them, your
html_entity_decode
will simply revert back to their original status (which is encoded).