4

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<strlen($uni_str); $i+=4)
    {
        $text_str .= "&#x".substr($uni_str,$i,4).";";
    }

as this code just solves the problem of viewing the result in html page ,

but i want to but the result in php variable .

as the result of the code above was like

&#x0628;&#x0628;&#x0020;&#x0020;&#x0628;&#x0628;
user1372074
  • 43
  • 1
  • 5

3 Answers3

3

I found the solution , hope to help:

function uni2arabic($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

1

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'))
bobince
  • 528,062
  • 107
  • 651
  • 834
0

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

Code :

if (!function_exists('codepoint_encode')) {

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

}

if (!function_exists('codepoint_decode')) {

    function codepoint_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?.

Community
  • 1
  • 1
John Slegers
  • 45,213
  • 22
  • 199
  • 169