PHPで必要なjavascript関数があります。これが私のJavaScript関数です:
<script type="text/javascript">
str = '242357de5b105346ea2059795682443';
str_overral = str;
str_overral = str_overral.replace(/[^a-z0-9]/gi, '').toLowerCase();
str_res='';
for (i=0; i<str_overral.length; i++) {
l=str_overral.substr(i,1);
d=l.charCodeAt(0);
if ( Math.floor(d/2) == d/2 ) {
str_res+=l;
} else {
str_res=l+str_res;
}
}
document.write('<in');
document.write('put type="hidden" name="myInput" value="'+str_res+'" />');
</script>
以上の JavaScript 関数は、myInput に対して次の文字列を生成します。359795ae3515e753242db0462068244
そして、これをPHPで試しました:
$str = '242357de5b105346ea2059795682443';
$str_overral = preg_replace('/[^a-z0-9]/i', '',$str);
$str_overral = strtolower($str_overral);
$str_res='';
for ($i=0; $i<strlen($str_overral); $i++) {
$l= substr($str_overral,$i,1);
// PHP does not have charCodeAt() function so i used uniord()
$d = uniord($l);
if((floor($d)/2) == ($d/2))
$str_res.=$l;
else
$str_res.= $l.$str_res;
}
echo $str_res;
function uniord($c) {
$h = ord($c{0});
if ($h <= 0x7F) {
return $h;
} else if ($h < 0xC2) {
return false;
} else if ($h <= 0xDF) {
return ($h & 0x1F) << 6 | (ord($c{1}) & 0x3F);
} else if ($h <= 0xEF) {
return ($h & 0x0F) << 12 | (ord($c{1}) & 0x3F) << 6
| (ord($c{2}) & 0x3F);
} else if ($h <= 0xF4) {
return ($h & 0x0F) << 18 | (ord($c{1}) & 0x3F) << 12
| (ord($c{2}) & 0x3F) << 6
| (ord($c{3}) & 0x3F);
} else {
return false;
}
}
以上の PHP コードはこの文字列を生成します。242357de5b105346ea2059795682443
したがって、基本的に PHP は $string をそのまま返します。
PHP には charCodeAt() 関数がないため、ここで解決策を見つけましたUTF-8 Safe Equivelant of ord または charCodeAt() in PHP、しかしそれは私にとってはうまくいきません。同じスレッドで「hakre」によって投稿された解決策も試しました。
どんな種類の助けにも感謝します。
更新ソリューション:
ここに修正がありました:
if($d%2 == 0)
$str_res.=$l;
else
$str_res = $l.$str_res;