I have simple code in JS that I cannot replicate to PHP when it comes to special characters.
This is the JS code (see JSFiddle for output):
var str = "t๐๐ฟ๐๐โ๏ธ๐๐จ๐ฌ๐ฏฆ";
document.write("Length is: "+str.length);
for(var i=0; i<str.length; i++) {
document.write("<br> charCodeAt(" + i + "): " + str.charCodeAt(i));
}
PHP strlen()already gives a different result, but I managed to get the same thing with a custom function JS_StringLength(thanks to this SO answer).
Here is what I have in PHP so far (see Sandbox for output):
<?php
function JS_StringLength($string) {
return strlen(iconv('UTF-8', 'UTF-16LE', $string)) / 2;
}
function JS_charCodeAt($str, $index){
$char = mb_substr($str, $index, 1, 'UTF-8');
if (mb_check_encoding($char, 'UTF-8'))
{
$ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
return hexdec(bin2hex($ret));
} else {
return null;
}
}
$str = "t๐๐ฟ๐๐โ๏ธ๐๐จ๐ฌ๐ฏฆ";
echo $str."\n";
echo "Length is: ".JS_StringLength($str)."\n";
for($i=0; $i<JS_StringLength($str); $i++) {
echo "charCodeAt(".$i."): ".JS_charCodeAt($str, $i)."\n";
}
However, after a whole day, Google and everything I found, did not give the same results as JS.
Please help me, I have no more ideas!
What should JS_charCodeAtget the same result as JS?
EDIT:
, javascript PHP? charCodeAt, , , emojis.
Update1:
, :
https://r12a.imtqy.com/apps/conversion/ ( ). , JS UTF-16, PHP UTF-8. php UTF-16, hex- > dec ? , ?
UPDATE2:
, .., , , json_encode() , (facepalm), - , JavaScript "". !