嗨,我正在尝试将 javascript 代码转换为 php.below 是 javascript 代码
<script>
var prefs = '["w",32,"r","s",2,"w",65,"w",26,"w",45,"w",24,"w",40,"s",2]';
var si = "hellocode";
var sig = (si + "").split("");
function swap(arr, b) {
l3 = arr[0];
l4 = arr[b % arr.length];
arr[0] = l4;
arr[b] = l3;
return arr
}
var cmd = JSON.parse(prefs||'["r","r"]');
cmd.forEach(function (c, i) {
if (typeof c !== "string") {
return;
}
switch (c) {
case "r":
sig = sig.reverse();
break;
case "s":
sig = sig.slice(cmd[i+1]);
break;
case "w":
sig = swap(sig, cmd[i+1]);
break;
}
});
</script>
到目前为止我在 php 中尝试过的内容
<?php
$prefs = '["w",32,"r","s",2,"w",65,"w",26,"w",45,"w",24,"w",40,"s",2]';
$si = "hellocode";
$arr = str_split($si);
$sig = implode(',', $arr);
function swap($arr, $b) {
$cool = explode(",",$arr);
$l3 = $cool[0];
$l4 = $cool[$b % length($cool)];
$cool[0] = $l4;
$cool[$b] =$l3;
return $cool;
}
// $cool = json_decode($prefs||'["r","r"]');
//now i am struck with converting $prefs to array
$i=0;
foreach($cool as $c)
{
// how to get type of string here ?
// And what does slice function do ?
}
我在第二部分被击中了?请参阅 js 到 php 的评论和一些专家 ..请指导我。:)