Trying to use php similar_text()
with arabic, but it's not working.
However it works great with english.
<?php
$var = similar_text("ياسر","عمار","$per");
echo $var;
?>
outbot : 5
that's wrong result, it should be 2. Is there similar_text()
with arabic letters?
Here's one I'm using
So
Just for the record and hopefully to make some help, I want to clarify the behavior of the
similar_text()
function when some multi-byte character strings are given (including the character strings of the Arabic.)The function simply treats each byte of the input string as an individual character (which implies it neither supports multi-byte characters nor the Unicode.)
The byte streams of the
عمار
andياسر
strings are respectively represented as the following (the bytes (in the hexadecimal representation) are separated using.
and, where the end of a character is reached, then a:
is used instead):As you can tell, there are five matching, and that's the reason why the function returns
5
in this case (every two hexadecimal digits represent a byte.)Because the Arabic text are multibyte strings normal PHP functions cannot be used (such as 'similar_text()').
The above code outputs: 8
Using the mb_strlen function with the UTF-8 encoding specified, the output is: 4 (the correct number of characters).
You can use the mb_ functions to make your own version of the similar_text function: http://php.net/manual/en/ref.mbstring.php