2

i try to replace some arabic caractere with another symbole some arbic caractere is replaced but one of this caractere it not replaced first i try to split the word second try to replace the caractere the caractere is َ ً ٍ ِ ْ ُ all this are replaced but the problem is with this caractere ّ strong text this is the code

   $spelling="";

function str_split_unicode($str, $l = 0) {

if ($l > 0) {

$ret = array();

$len = mb_strlen($str, "UTF-8");

for ($i = 0; $i < $len; $i += $l) {

$ret[] = mb_substr($str, $i, $l, "UTF-8");

}
return $ret;
}
return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
}

if(isset($_POST['txt_word'])){

 $spelling = implode("  ", str_split_unicode($_POST['txt_word']));

}


 $search  = array('ّّ ','ً ','ٍ ','ٌ ','ُ ','َ ','ِ ','ْ ');

$replace = array('0/','/0','/0','/0','/','/','/','0');

$subject= str_replace($search, $replace,  $spelling);

$alphabet=array('أ','ا','ب','ت','ث','ج','ح','خ','د','ذ','ر','ز','س','ش','س','ص','ط','ظ','ع','غ','ف','ق','ل','م','ن','ه','و','ي','لا','ة','إ','ئ','ؤ','ك','ى','ء');

$rep=array('','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','');

$res=str_replace($alphabet,$rep,$subject);
danmullen
  • 2,556
  • 3
  • 20
  • 28
user2855148
  • 25
  • 1
  • 8
  • possible duplicate of [PHP Multi Byte str\_replace?](http://stackoverflow.com/questions/1451144/php-multi-byte-str-replace) – feeela Nov 07 '14 at 13:07

2 Answers2

0

str_replace() should be able to handle Unicode characters If it does not, you are probably not passing it Unicode.

Make sure that you are handling Unicode strings all the way through from the displayed website to the PHP script. See this article for further information: „Unicode-friendly PHP and MySQL“.

feeela
  • 29,399
  • 7
  • 59
  • 71
0

I have used http://www.php.net/preg_replace on similar cases.

$replace = array(
   '/ä/u'=>'a',
   '/p/u'=>'r',
);

$patterns = array_keys($replace);
$replacements = array_values($replace);

echo preg_replace($patterns, $replacements, $string);
MTJ
  • 1,059
  • 1
  • 10
  • 23