Skip to content Skip to sidebar Skip to footer

How To Convert Unicode To Arabic Characters In Php?

let us say that the string is $uni_str='06280628002006280628'; In Arabic,it is: بب بب so , how can i convert it in php without using html like: for($i=0; $i

Solution 1:

I found the solution , hope to help:

functionuni2arabic($uni_str) 
{   
      for($i=0; $i<strlen($uni_str); $i+=4)
         {
                $new="&#x".substr($uni_str,$i,4).";"; 
                $txt = html_entity_decode("$new", ENT_COMPAT, "UTF-8");
                $All.=$txt;
         }

    return$All;
} 

variable $All contains the arabic string

Solution 2:

Use hex2bin to decode the hex into a sequence of bytes, and then you can unpack each pair of bytes as a UTF-16 code unit (which is what I assume your string represents).

Assuming you are producing UTF-8 text output:

iconv('UTF-16BE', 'UTF-8', hex2bin('06280628002006280628'))

Solution 3:

The following code allows you to decode the characters as well as re-encode them if necessary

Code :

if (!function_exists('codepoint_encode')) {

    functioncodepoint_encode($str) {
        return substr(json_encode($str), 1, -1);
    }

}

if (!function_exists('codepoint_decode')) {

    functioncodepoint_decode($str) {
        return json_decode(sprintf('"%s"', $str));
    }

}

How to use :

header('Content-Type: text/html; charset=utf-8'); 

var_dump(codepoint_encode('ඔන්ලි'));
var_dump(codepoint_encode('සින්ග්ලිෂ්'));

var_dump(codepoint_decode('\u0d94\u0db1\u0dca\u0dbd\u0dd2'));
var_dump(codepoint_decode('\u0dc3\u0dd2\u0db1\u0dca\u0d9c\u0dca\u0dbd\u0dd2\u0dc2\u0dca'));

Output :

string(30) "\u0d94\u0db1\u0dca\u0dbd\u0dd2"string(60) "\u0dc3\u0dd2\u0db1\u0dca\u0d9c\u0dca\u0dbd\u0dd2\u0dc2\u0dca"string(15) "ඔන්ලි"string(30) "සින්ග්ලිෂ්"

If you want more complex functionality, see How to get the character from unicode code point in PHP?.

Post a Comment for "How To Convert Unicode To Arabic Characters In Php?"