How to convert unicode to arabic characters in php?

非 Y 不嫁゛ 提交于 2019-12-12 15:28:41

问题


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;

回答1:


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




回答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'))



回答3:


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



来源:https://stackoverflow.com/questions/15932300/how-to-convert-unicode-to-arabic-characters-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!