+2 votes
in Programming Languages by (7.4k 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?


string with HTML entities 

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


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

1 Answer

0 votes
by (30.3k points)

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

Here is an example:

$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>