phpstringhtml-entitiesstring-conversioncharacter-entities

How to convert html entities of greek language to actual symbols in php


I have greek site and all posts in it looks in sourse like:

<p>&Omicron; &Mu;&pi;&#972;&nu;&omicron;&upsilon;&sigmaf; &Gamma;&#973;&rho;&omicron;&sigmaf; Drunken Clam &theta;&alpha; &delta;&epsilon;&#943;&tau;&epsilon; &theta;&alpha; &epsilon;&#943;&nu;&alpha;&iota; &sigma;&epsilon; &theta;&#941;&sigma;&eta; &nu;&alpha; &kappa;&epsilon;&rho;&delta;&#943;&zeta;&epsilon;&iota; &#972;&sigma;&omicron; 1000&Chi; &tau;&omicron;&upsilon; &sigma;&upsilon;&nu;&omicron;&lambda;&iota;&kappa;&omicron;&#973; &sigma;&tau;&omicron;&iota;&chi;&#942;&mu;&alpha;&tau;&omicron;&sigmaf; &epsilon;&pi;&iota;&lambda;&#941;&gamma;&omicron;&nu;&tau;&alpha;&sigmaf; &epsilon;&#943;&tau;&epsilon; Cleveland, Peter, Quagmire, &kappa;&alpha;&iota;</p>

Actually it looks like this on page like this:

Ο Μπόνους Γύρος Drunken Clam θα δείτε θα είναι σε θέση να κερδίζει όσο 1000Χ του συνολικού στοιχήματος επιλέγοντας είτε Cleveland, Peter, Quagmire, και

I need to change html entities to actual symbols (I'm using UTF-8 encoding.) Seems html_entity_decode() doesn't do this. Is here any library or somenthing like this for me to be able to convert text to actual symbols.

PHP version PHP 5.3.29

Thanks!

Upadate: Sorry, this was my shameful mistake. I messed mb_internal_encoding() and 'default_charset' ini directive while was debugging my issue. So I was just have to set encoding for html_entity_decode work as third parameter:

   $decoded = html_entity_decode($str, ENT_COMPAT | ENT_HTML401, "UTF-8");

just as Charlotte Dunois wrote. So I set her answer as correct.


Solution

  • If your data is html encoded (e.g. with PHP's function htmlentities), you can use html_entity_decode to decode it back. You may need to set the charset argument if your set charset in the php.ini differs.

    https://3v4l.org/Lmt38