阿拉伯字符转ABCD字符
arabic characters to ABCD characters
我正在 php 中使用以下代码来显示我代码中定义的罗马 ABCD
字符中的阿拉伯字母。
但是显示不正确。它也失去了字符排序,并且没有根据我的字符串显示某些字符。
显示为 _space_aabtkhlmn
,应该显示为 khatm_space_alanbyaa
。
我想不通我哪里错了。
请问为什么显示错误?
<!DOCTYPE HTML>
<head>
<?php header('Content-type: text/html; charset=utf-8'); ?>
</head>
<body>
<?php
echo $mystr = "خاتم الانبیاء";
echo "<hr>";
$empty = " ";
$a = "ء";
$a1 = "ا";
$a2 = "آ";
$b = "ب";
$c = "پ";
$d = "ة";
$e = "ت";
$ea = "ٹ";
$f = "ث";
$g = "ج";
$h = "چ";
$ha = "ح";
$i = "خ";
$j = "د";
$ja = "ڈ";
$k = "ذ";
$l = "ر";
$m = "ڑ";
$ma = "ز";
$mb = "ژ";
$n = "س";
$na = "ش";
$nb = "ص ";
$nc = "ض";
$o = "ط";
$p = "ظ";
$q = "ع";
$r = "غ";
$s = "ف";
$t = "ق";
$ta = "ک";
$tb = "گ";
$u = "ل";
$v = "م";
$w = "ن";
$wa = "ں";
$x = "ہ";
$xa = "ھ";
$y = "و";
$ya = "ے";
$yb = "ى";
$yc = "ي";
$me = preg_split('//u', $mystr);
$imp = implode(",", $me);
echo "<div style='direction: ltr;'>";
if(stripos($imp, $empty) == true){ echo "_space_"; }
if(stripos($imp, $a) == true){ echo "a"; }
if(stripos($imp, $a1) == true){ echo "a";}
if(stripos($imp, $a2) == true){ echo "aa";}
if(stripos($imp, $b) == true){ echo "b";}
if(stripos($imp, $c) == true){ echo "p";}
if(stripos($imp, $d) == true){ echo "h";}
if(stripos($imp, $e) == true){ echo "t";}
if(stripos($imp, $ea) == true){ echo "t";}
if(stripos($imp, $f) == true){ echo "s";}
if(stripos($imp, $g) == true){ echo "j";}
if(stripos($imp, $h) == true){ echo "ch";}
if(stripos($imp, $ha) == true){ echo "h";}
if(stripos($imp, $i) == true){ echo "kh";}
if(stripos($imp, $j) == true){ echo "d";}
if(stripos($imp, $ja) == true){ echo "d";}
if(stripos($imp, $k) == true){ echo "z";}
if(stripos($imp, $l) == true){ echo "r";}
if(stripos($imp, $m) == true){ echo "rr";}
if(stripos($imp, $ma) == true){ echo "z";}
if(stripos($imp, $mb) == true){ echo "x";}
if(stripos($imp, $n) == true){ echo "s";}
if(stripos($imp, $na) == true){ echo "sh";}
if(stripos($imp, $nb) == true){ echo "s";}
if(stripos($imp, $nc) == true){ echo "d";}
if(stripos($imp, $o) == true){ echo "t";}
if(stripos($imp, $p) == true){ echo "z";}
if(stripos($imp, $q) == true){ echo "u";}
if(stripos($imp, $r) == true){ echo "gh";}
if(stripos($imp, $s) == true){ echo "f";}
if(stripos($imp, $t) == true){ echo "q";}
if(stripos($imp, $ta) == true){ echo "k";}
if(stripos($imp, $tb) == true){ echo "g";}
if(stripos($imp, $u) == true){ echo "l";}
if(stripos($imp, $v) == true){ echo "m";}
if(stripos($imp, $w) == true){ echo "n";}
if(stripos($imp, $wa) == true){ echo "n";}
if(stripos($imp, $x) == true){ echo "h";}
if(stripos($imp, $xa) == true){ echo "h";}
if(stripos($imp, $y) == true){ echo "o";}
if(stripos($imp, $ya) == true){ echo "y";}
if(stripos($imp, $yb) == true){ echo "y";}
if(stripos($imp, $yc) == true){ echo "y";}
echo "</div>";
echo "<hr>";
?>
</body>
</html>
您的逻辑将字符串拆分为以逗号分隔的字符。然后它检查 space 是否在字符串中的任何地方,如果是,则打印 _space_
,然后检查 Ç 是否在字符串中的任何地方,如果是,则打印“a”,然后检查是否 ا是字符串中的任何位置,如果是,则打印“aa”。这将按照您测试的顺序打印结果,而不是字符串的顺序。
我相信你的意思是这样的:
$mystr = str_replace($empty, "_space_", $mystr);
$mystr = str_replace($a, "a", $mystr);
$mystr = str_replace($a1, "a", $mystr);
$mystr = str_replace($a2, "aa", $mystr);
$mystr = str_replace($b, "b", $mystr);
$mystr = str_replace($c, "p", $mystr);
$mystr = str_replace($d, "h", $mystr);
$mystr = str_replace($e, "t", $mystr);
$mystr = str_replace($ea, "t", $mystr);
$mystr = str_replace($f, "s", $mystr);
...
除了您的逻辑中的一个小问题外,这行得通。您的字符串包含波斯语 (U+06CC),但您不检查那个。您只检查 (U+06D2;乌尔都语?我不知道这个)、ى(U+0649 Alef Maksura)和 ي(U+064A 阿拉伯语)。所以你需要另一行:
$yd = "ی"; // ARABIC LETTER FARSI YEH (U+06CC)
...
$mystr = str_replace($yd, "y", $mystr);
您是否打算用“a”而不是“y”替换 Alef Maksura?
我正在 php 中使用以下代码来显示我代码中定义的罗马 ABCD
字符中的阿拉伯字母。
但是显示不正确。它也失去了字符排序,并且没有根据我的字符串显示某些字符。
显示为 _space_aabtkhlmn
,应该显示为 khatm_space_alanbyaa
。
我想不通我哪里错了。
请问为什么显示错误?
<!DOCTYPE HTML>
<head>
<?php header('Content-type: text/html; charset=utf-8'); ?>
</head>
<body>
<?php
echo $mystr = "خاتم الانبیاء";
echo "<hr>";
$empty = " ";
$a = "ء";
$a1 = "ا";
$a2 = "آ";
$b = "ب";
$c = "پ";
$d = "ة";
$e = "ت";
$ea = "ٹ";
$f = "ث";
$g = "ج";
$h = "چ";
$ha = "ح";
$i = "خ";
$j = "د";
$ja = "ڈ";
$k = "ذ";
$l = "ر";
$m = "ڑ";
$ma = "ز";
$mb = "ژ";
$n = "س";
$na = "ش";
$nb = "ص ";
$nc = "ض";
$o = "ط";
$p = "ظ";
$q = "ع";
$r = "غ";
$s = "ف";
$t = "ق";
$ta = "ک";
$tb = "گ";
$u = "ل";
$v = "م";
$w = "ن";
$wa = "ں";
$x = "ہ";
$xa = "ھ";
$y = "و";
$ya = "ے";
$yb = "ى";
$yc = "ي";
$me = preg_split('//u', $mystr);
$imp = implode(",", $me);
echo "<div style='direction: ltr;'>";
if(stripos($imp, $empty) == true){ echo "_space_"; }
if(stripos($imp, $a) == true){ echo "a"; }
if(stripos($imp, $a1) == true){ echo "a";}
if(stripos($imp, $a2) == true){ echo "aa";}
if(stripos($imp, $b) == true){ echo "b";}
if(stripos($imp, $c) == true){ echo "p";}
if(stripos($imp, $d) == true){ echo "h";}
if(stripos($imp, $e) == true){ echo "t";}
if(stripos($imp, $ea) == true){ echo "t";}
if(stripos($imp, $f) == true){ echo "s";}
if(stripos($imp, $g) == true){ echo "j";}
if(stripos($imp, $h) == true){ echo "ch";}
if(stripos($imp, $ha) == true){ echo "h";}
if(stripos($imp, $i) == true){ echo "kh";}
if(stripos($imp, $j) == true){ echo "d";}
if(stripos($imp, $ja) == true){ echo "d";}
if(stripos($imp, $k) == true){ echo "z";}
if(stripos($imp, $l) == true){ echo "r";}
if(stripos($imp, $m) == true){ echo "rr";}
if(stripos($imp, $ma) == true){ echo "z";}
if(stripos($imp, $mb) == true){ echo "x";}
if(stripos($imp, $n) == true){ echo "s";}
if(stripos($imp, $na) == true){ echo "sh";}
if(stripos($imp, $nb) == true){ echo "s";}
if(stripos($imp, $nc) == true){ echo "d";}
if(stripos($imp, $o) == true){ echo "t";}
if(stripos($imp, $p) == true){ echo "z";}
if(stripos($imp, $q) == true){ echo "u";}
if(stripos($imp, $r) == true){ echo "gh";}
if(stripos($imp, $s) == true){ echo "f";}
if(stripos($imp, $t) == true){ echo "q";}
if(stripos($imp, $ta) == true){ echo "k";}
if(stripos($imp, $tb) == true){ echo "g";}
if(stripos($imp, $u) == true){ echo "l";}
if(stripos($imp, $v) == true){ echo "m";}
if(stripos($imp, $w) == true){ echo "n";}
if(stripos($imp, $wa) == true){ echo "n";}
if(stripos($imp, $x) == true){ echo "h";}
if(stripos($imp, $xa) == true){ echo "h";}
if(stripos($imp, $y) == true){ echo "o";}
if(stripos($imp, $ya) == true){ echo "y";}
if(stripos($imp, $yb) == true){ echo "y";}
if(stripos($imp, $yc) == true){ echo "y";}
echo "</div>";
echo "<hr>";
?>
</body>
</html>
您的逻辑将字符串拆分为以逗号分隔的字符。然后它检查 space 是否在字符串中的任何地方,如果是,则打印 _space_
,然后检查 Ç 是否在字符串中的任何地方,如果是,则打印“a”,然后检查是否 ا是字符串中的任何位置,如果是,则打印“aa”。这将按照您测试的顺序打印结果,而不是字符串的顺序。
我相信你的意思是这样的:
$mystr = str_replace($empty, "_space_", $mystr);
$mystr = str_replace($a, "a", $mystr);
$mystr = str_replace($a1, "a", $mystr);
$mystr = str_replace($a2, "aa", $mystr);
$mystr = str_replace($b, "b", $mystr);
$mystr = str_replace($c, "p", $mystr);
$mystr = str_replace($d, "h", $mystr);
$mystr = str_replace($e, "t", $mystr);
$mystr = str_replace($ea, "t", $mystr);
$mystr = str_replace($f, "s", $mystr);
...
除了您的逻辑中的一个小问题外,这行得通。您的字符串包含波斯语 (U+06CC),但您不检查那个。您只检查 (U+06D2;乌尔都语?我不知道这个)、ى(U+0649 Alef Maksura)和 ي(U+064A 阿拉伯语)。所以你需要另一行:
$yd = "ی"; // ARABIC LETTER FARSI YEH (U+06CC)
...
$mystr = str_replace($yd, "y", $mystr);
您是否打算用“a”而不是“y”替换 Alef Maksura?