2

I am working with below code in php to show the arabic letters in roman ABCD characters as defined below in my code.

But it is not displaying properly. It is losing the character sorting also and not displaying some of the characters according to my string.

it is showing as _space_aabtkhlmn and it should show as khatm_space_alanbyaa.

I can not figure it out where i am wrong.

Please help why it is showing wrong?

<!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>
usman610
  • 479
  • 1
  • 5
  • 22
  • Note: there is a supplicate question, with alternate solutions: https://stackoverflow.com/questions/18927852/transliterate-arabic-characters-into-english-wordpress – Giacomo Catenazzi Sep 08 '21 at 05:46

1 Answers1

0

Your logic splits the string into characters separated by commas. It then checks if space is anywhere in the string, and if so print _space_, and then checks if ء is anywhere in the string, and if so, print "a", and then checks if ا is anywhere in the string, and if so, print "aa." This is going to print the results in the order you test in, not the order of the string.

What I believe you meant to do is this:

$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);
...

This works, except for a small problem in your logic. Your string includes the Farsi ی (U+06CC), but you don't check for that one. You only check for ے (U+06D2; Urdu? I don't know this one), ى (U+0649 Alef Maksura), and ي (U+064A Arabic). So you'd need another line:

$yd = "ی"; // ARABIC LETTER FARSI YEH (U+06CC)
...
$mystr = str_replace($yd, "y", $mystr);

Is it possible you meant to replace Alef Maksura with "a" rather than "y"?

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • thanks alot thaats what exactly i want thanks again – usman610 Sep 07 '21 at 16:32
  • one question please. how to extract or is it possible to get the full arabic word or words from `$mystr` to match with another string `$anotherstr` and display something else other than full matching word / words? – usman610 Sep 07 '21 at 17:59
  • 1
    I assume you mean something like `explode(" ", $mystr)`? I depends on what you consider a word break. `preg_split` could do it too, splitting on `\s` (whitespace) or `\W` (non-word characters) or the like. – Rob Napier Sep 07 '21 at 18:04