Friday, June 1, 2012

Decoding sign Euro in HTML & PHP


Probably you had a problem to show a Euro sign on a web page like on the next figure.


Code source:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title></title>
    </head>
    <body>
        <?php
        $str = "Price: 99,99€";
        echo htmlentities($str)."<br />";
        echo html_entity_decode($str)."<br />";
        ?>
    </body>
</html>


It happens because web page can have a charset setted UTF-8  (<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">or a ISO-8859-1 (<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">). 
To show correctly sign Euro, you must set charset ISO-8859-15 (<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15">) as you can see next picture.


Besides you set charset ISO-8859-15, you must use PHP function html_entity_decode(string) but must not use PHP function htmlentities(string).




1 comment: