I was trying range(); function with non-English language. It is not working.
$i =0
foreach(range('क', 'म') as $ab) {
++$i;
$alphabets[$ab] = $i;
}
Output: à =1
It was Hindi (India) alphabets. It is only iterating only once (Output shows).
For this, I am not getting what to do!
So, if possible, please tell me what to do for this and what should I do first before thinking of working with non-English text with any PHP functions.
Short answer: it’s not possible to use
rangelike that.Explanation
You are passing the string ‘क’ as the start of the range and ‘म’ as the end. You are getting only one character back, and that character is
à.You are getting back
àbecause your source file is encoded (saved) in UTF-8. One can tell this by the fact thatàis code pointU+00E0, while0xE0is also the first byte of the UTF-8 encoded form of ‘क’ (which is0xE0 0xA4 0x95). Sadly, PHP has no notion of encodings so it just takes the first byte it sees in the string and uses that as the “start” character.You are getting back only
àbecause the UTF-8 encoded form of ‘म’ also starts with0xE0(so PHP also thinks that the “end character” is0xE0orà).Solution
You can write
rangeas aforloop yourself, as long as there is some function that returns the Unicode code point of an UTF-8 character (and one that does the reverse). So I googled and found these here:With the above, you can now write:
See it in action.