+2 votes
in Programming Languages by (8.2k points)

I am trying to read some web pages, but my code returns some HTML entities instead of their equivalent character. How can I convert those HTML entities to their equivalent characters?

E.g.

string with HTML entities 

'<a href="https://www.youtube.com">youtube.com</a>'

to 

<a href="https://www.youtube.com">youtube.com</a>

1 Answer

0 votes
by (31.6k points)

You can use the PHP function html_entity_decode() to convert HTML entities to characters.

Here is an example:

<?php
$str = '&lt;a href=&quot;https://www.youtube.com&quot;&gt;youtube.com&lt;/a&gt;';
echo html_entity_decode($str);
?>

The above code will return the following string:

<a href="https://www.youtube.com">youtube.com</a>

...