3

I have a string as "€".

I want to convert it to hex to get the value as "\u20AC" so that I can send it to flash.

Same for all currency symbol..

 £  ->  \u00A3
 $ ->  \u0024
 etc
Priya
  • 1,522
  • 1
  • 14
  • 31

2 Answers2

3

First, note that $ is not a known entity in HTML 4.01. It is, however, in HTML 5, and, in PHP 5.4, you can call html_entity_decode with ENT_QUOTES | ENT_HTML5 to decode it.

You have to decode the entity and only then convert it:

//assumes $str is in UTF-8 (or ASCII)
function foo($str) {
    $dec = html_entity_decode($str, ENT_QUOTES, "UTF-8");
    //convert to UTF-16BE
    $enc = mb_convert_encoding($dec, "UTF-16BE", "UTF-8");
    $out = "";
    foreach (str_split($enc, 2) as $f) {
        $out .= "\\u" . sprintf("%04X", ord($f[0]) << 8 | ord($f[1]));
    }
    return $out;
}

If you want to replace only the entities, you can use preg_replace_callback to match the entities and then use foo as a callback.

function repl_only_ent($str) {
    return preg_replace_callback('/&[^;]+;/',
        function($m) { return foo($m[0]); },
    $str);
}

echo repl_only_ent("&euro;foobar &acute;");

gives:

\u20ACfoobar \u00B4
Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • my php version is 5.1.6...its showing fatal error at mb_convert_encoding ...how can i get the value please.... – Priya Sep 20 '11 at 10:27
  • @PRA You can use iconv instead of mbstring, if you wish. If you don't have iconv too, you'll have to convert UTF-8 to UTF-16 manually. – Artefacto Sep 20 '11 at 10:31
  • i have tried iconv() but i am unable to recognize the charset type to convert hex – Priya Sep 20 '11 at 10:34
-1

You might try the following function for string to hex conversion:

function strToHex($string) {
    $hex='';
    for ($i=0; $i < strlen($string); $i++) {
        $hex .= dechex(ord($string[$i]));
    }
    return $hex;
}

From Greg Winiarski which is the fourth hit on Google.

In combination with html_entity_decode(). So something like this:

$currency_symbol = "&euro;";
$hex = strToHex(html_entity_decode($currency_symbol));

This code is untested and therefore may require further modification to return the exact result you require

Treffynnon
  • 21,365
  • 6
  • 65
  • 98
  • -1 only works with code points <= U+00FF (fails with €, for instance) and even then it doesn't add \u00. Plus, to convert a string to hex, there's bin2hex, no need for your function. – Artefacto Sep 20 '11 at 09:58
  • BTW, it will fail even more spectacularly in PHP 5.4, when the default for html_entity_decode becomes UTF-8 and not ISO-8859-1. – Artefacto Sep 20 '11 at 10:03