Javascript to PHP domain.charCodeAt(i)
javascript, php
Solution
This should be a Unicode safe version.
$domain = "example.com";
$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 comment on the `ord` documentation.
Problem
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.com"; $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.