/ / Convertendo a função javascript para PHP - php, javascript, function

Convertendo a função javascript para PHP - php, javascript, function

Eu estou tentando converter esta função:

function txt(str){
var t=""; var s=unescape(str);
var x=Math.round(Math.sqrt(16));
for(var i=0;i<s.length;i++) t+=
String.fromCharCode(s.charCodeAt(i)^(i%2?x:0));
document.write(t.replace(/*/g,"</td><td>").replace(/^/g,"" title="").replace(/~/g,"</td></tr><tr class="dbeven"><td>").replace(/!/g,"</td></tr><tr class="dbodd"><td>").
replace(/|/g,"</td><td><a href="/cgi-bin/shdb.pl?key=")
);
}

para php, isso é o que eu tentei:

function Txt($str)
{
$t = "";
$s = urldecode($str);
$x = round(sqrt(16));
for($i=0;$i<count($str);$i++)
{
$t = $t . chr(charCodeAt($str, ($i^($i%2?x:0))));
$t = str_replace("*", "</td><td>", $t);
$t = str_replace("^", "" title="", $t);
$t = str_replace("~", "</td></tr><tr class="dbeven"><td>", $t);
$t = str_replace("!", "</td></tr><tr class="dbodd"><td>", $t);
$t = str_replace("|", "</td><td><a href="/cgi-bin/shdb.pl?key=", $t);
}
echo $t;
}

function charCodeAt($str, $i){
return ord(substr($str, $i, 1));
}

Mas não está funcionando, alguém pode me ajudar? obrigado

Respostas:

1 para resposta № 1

Você tem erros no seu str_replace(). Você deve substituir *, ^, | chars, não *, ^, |. é um caractere de escape em expressões regulares