How to convert JavaScript code to its PHP equivalent?

不羁岁月 提交于 2019-12-31 06:41:29

问题


I have js code:

var b = "aHR0cDovL3d3dy5oZHpvZy5jb20vZ2V0X2ZpbGUvМS84Y2Е5МTЕ4ZmМyNmVkNTk0ZmI5Yzc2ZWI2Y2Y2YWVmМС85NDАwМС85NDU4Ny85NDU4Ny5tcDQvP3RpbWU9МjАxNzА5МjYyМDIxNDYmcz05МTUzZmNmYjАyOTUyOWQxY2JhZTВkYzNkY2ZhODVmZiZicj0xODЕ1JmQ9МTcwNyZmPXZpZGVvLm0zdTg~";
var f = "\u0410\u0412\u0421D\u0415FGHIJKL\u041cNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,~".indexOf(b.charAt(0));
document.write(f);

It return 26. And I convert js code to php code:

$url = 'aHR0cDovL3d3dy5oZHpvZy5jb20vZ2V0X2ZpbGUvМS84Y2Е5МTЕ4ZmМyNmVkNTk0ZmI5Yzc2ZWI2Y2Y2YWVmМС85NDАwМС85NDU4Ny85NDU4Ny5tcDQvP3RpbWU9МjАxNzА5МjYyМDIxNDYmcz05МTUzZmNmYjАyOTUyOWQxY2JhZTВkYzNkY2ZhODVmZiZicj0xODЕ1JmQ9МTcwNyZmPXZpZGVvLm0zdTg~';
$str = "\u0410\u0412\u0421D\u0415FGHIJKL\u041cNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,~";
$str = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) {
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}, $str);
$f = strpos($str, $url[0]);
echo $f;

But it return 31. Why?


回答1:


'\u0421' in JavaScript is representative of a character. In Php however, it is not. That's why JavaScript is showing fewer spaces between index 0 and the first instance of 'a'.

To remedy this... I'm pretty sure this is what you're looking for... How to decode Unicode escape sequences like "\u00ed" to proper UTF-8 encoded characters?



来源:https://stackoverflow.com/questions/46439056/how-to-convert-javascript-code-to-its-php-equivalent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!