admin管理员组文章数量:1326078
I'm trying to convert a javascript snippet into PHP. Javascript is
var sum1 = 0, sum2 = 0;
for (var i = domain.length - 1; i >= 0; i--) {
sum1 += domain.charCodeAt(i) * 13748600747;
sum2 += domain.charCodeAt(i) * 40216416130;
}
var x = ("$" + sum1).substring(0, 8) + ("" + sum2).substring(0, 8);
But couldn't understand this part, sum1 += domain.charCodeAt(i) * 13748600747;
I mean which PHP function can be used instead of domain.charCodeAt(i).
EDIT:
My PHP code:
$domain = "example";
$sum1 = 0;
$sum2 = 0;
$length = strlen($domain);
$i = $length - 1;
for ( $i; $i >= 0; $i-- ) {
$sum1 += ord($domain[$i]) * 13748600747;
$sum2 += ord($domain[$i]) * 40216416130;
}
$newsum = "$".$sum1;
$x = substr($newsum,0,8) + substr($sum2,0,8);
echo $x;
Output is definitely different. Need Help.
I'm trying to convert a javascript snippet into PHP. Javascript is
var sum1 = 0, sum2 = 0;
for (var i = domain.length - 1; i >= 0; i--) {
sum1 += domain.charCodeAt(i) * 13748600747;
sum2 += domain.charCodeAt(i) * 40216416130;
}
var x = ("$" + sum1).substring(0, 8) + ("" + sum2).substring(0, 8);
But couldn't understand this part, sum1 += domain.charCodeAt(i) * 13748600747;
I mean which PHP function can be used instead of domain.charCodeAt(i).
EDIT:
My PHP code:
$domain = "example.";
$sum1 = 0;
$sum2 = 0;
$length = strlen($domain);
$i = $length - 1;
for ( $i; $i >= 0; $i-- ) {
$sum1 += ord($domain[$i]) * 13748600747;
$sum2 += ord($domain[$i]) * 40216416130;
}
$newsum = "$".$sum1;
$x = substr($newsum,0,8) + substr($sum2,0,8);
echo $x;
Output is definitely different. Need Help.
Share Improve this question edited Mar 17, 2013 at 4:28 LuckyCoder asked Mar 17, 2013 at 4:05 LuckyCoderLuckyCoder 5209 silver badges29 bronze badges 2- 3 Note: JavaScript strings are stored as UTF-16 codes. For the equivalent in PHP, stackoverflow./q/155514 may help. – Jonathan Lonowski Commented Mar 17, 2013 at 4:14
-
Regarding your edit, you just have a typo.
+
should be.
--$x = substr($newsum,0,8) . substr($sum2,0,8);
– Jonathan Lonowski Commented Mar 17, 2013 at 5:36
3 Answers
Reset to default 4This should be a Unicode safe version.
$domain = "example.";
$sum1 = 0;
$sum2 = 0;
// this will convert $domain to a UTF-16 string,
// without specifying the third parameter, PHP will
// assume the string uses PHP's internal encoding,
// you might want to explicitly set the `from_encoding`
$domain = mb_convert_encoding($domain, 'UTF-16');
$length = mb_strlen($domain, 'UTF-16');
$i = $length - 1;
for ( $i; $i >= 0; $i-- ) {
$char = mb_substr($domain, $i, 1, 'UTF-16');
$sum1 += hexdec(bin2hex($char)) * 13748600747;
$sum2 += hexdec(bin2hex($char)) * 40216416130;
}
$newsum = "$" . strval($sum1);
$sum2 = strval($sum2);
$x = substr($newsum,0,8) . substr($sum2,0,8);
echo $x;
The conversion to decimal is based of the code in this ment on the ord
documentation.
Because ord() only works for ascii but javascript uses utf-16, we'd better use
hexdec(bin2hex($utf16_char))
Here is the experiment I did:
// This is a utf8 character because my editor can only accept utf8
$utf8_char = "北";
// convert to utf16 using mb_convert_encoding()
$utf16_char = mb_convert_encoding($utf8_char, "utf-16", "utf-8");
// output is correct: int(21271), same as javascript output
var_dump(hexdec(bin2hex($utf16_char)));
// output is wrong: int(83) because ord() only works for ascii character.
var_dump(ord($utf16_char));
Note: in order to pletely replace the above js code, you'd better use mbstring module and set utf-16 as the internal encoding.
mb_internal_encoding('utf-16');
Then use mb_* functions, e.g., use mb_strlen() instead of strlen(). Also you'd feed the php function with utf16 strings.
[UPDATE] To better understand the issue, note that the following 2 lines are equivalent:
hexdec(bin2hex($any_utf_char));
(int) base_convert(unpack("H*", $any_utf_char)[1], 16, 10); // Note: the array dereferencing only works in php5.4
That would be ord
($domain[$i])
本文标签: Javascript to PHP domaincharCodeAt(i)Stack Overflow
版权声明:本文标题:Javascript to PHP domain.charCodeAt(i) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742195905a2431074.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论