2

我正在使用 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>
4

1 回答 1

0

您的逻辑将字符串拆分为用逗号分隔的字符。然后它检查空格是否在字符串中的任何位置,如果是则 print _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?

于 2021-09-07T15:52:11.767 回答