naeemqaswar
4/20/2017 - 3:40 PM

Convert String to UNICODE

Function to geocode address, it will return false if unable to geocode address

// Reference: http://stackoverflow.com/questions/7482977/get-hexcode-of-html-entities

function convert_str_to_unicode($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
function convert_entities_to_unicode($str) {
    return preg_replace_callback('/&[^;]+;/',
        function($m) { return convert_str_to_unicode($m[0]); },
        $str);
}