0

how do you count the occurrences of a Unicode character in a string with PHP?
maybe this is a simple questions but I am a biginner in PHP. I want to count how many Unicode characters U+06cc are in a string.

Character 'yeh' in farsi corresponds to 2 code points.
ی = u+06cc
ي = u+064a
that u+064a is a substitute in Farsi.
The popular character Arabic charset CP-1256 has no character mapped into U+06cc.
now I want to count how many Unicode characters U+06cc are in a string to detect that string is arabic or farsi.
when I use $count = substr_count($str, "ى");
or when I use
$count = substr_count($str, "\xDB\x8c");
it counts both "ی" and "ي" ,
any idea ?

3
  • you should have written all this from the beginning... Anyway, what you write about substr_count($str, "\xDB\x8c") is not possible, if "ي" is "\xD9\x8A" (UTF-8 for U+064A). Jan 18, 2014 at 14:47
  • What is the encoding of your strings? Do you have mbstring active with "function overloading" (wrong term, but this is what they use) enabled? Jan 18, 2014 at 14:51
  • if you have a textfile with your text, and are able to do an od -tx1z of a line with the problem, maybe we can understand a bit more. And BTW, what I have written above about "not possible" is not necessarily 100% true, read my answer. Jan 18, 2014 at 15:33

2 Answers 2

0

I suppose you have a UTF-8 string, since UTF-8 is the most reasonable Unicode encoding.

$count = substr_count($str, "\xDB\x8C");

is what you want. You simply treat the string as a sequence of bytes. In UTF-8 the first byte of a multibyte character and its continuation bytes can never be mixed up (the first byte is always 11...... binary, while continuation bytes are always 10......). This ensures you cannot find something different from what your are looking for.

To find the UTF-8 encoding of U+06CC I used the fileformat.info website, which I think is the best for this purpose.

If you use UTF-8 in your IDE too, you can simply write "ى" instead of "\xDB\x8C" (internally they are exactly the same string in PHP), but that will make the readability of what you have written dependent on the IDE (often not good if you need to share your code).


Now that you have clarified your question, my above answer is no more appropriate. I leave it there just as a reference for other passers-by.

Your problem could stem from the fact that, reading here it seems that "ي" can lose its dots below if modified by the Unicode character U+0654 (the non-spacing mark "Arabic hamsa above"). Since my browser does not remove the dots, and adds the hamsa, I don't know whether the hamsa is supposed to disappear too when the dots disappear. Anyway, it COULD be that "\xDB\x8C" has the same appearance as "\xD9\x8A\xD9\x94". I have not been able to find the reverse, i.e., the double dot below as a non-spacing modification character, which would explain why substr_count($str, "\xDB\x8c") finds the Arabic yeh too - but maybe it exists.

0

I have tried this example, and it works fine:

$str="مىمى";
$count = substr_count($str, "ى");
echo $count;

I got the answer 2 , which is true.

If you want a more specific answer, you should provide more specific details in your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.