将javascript函数转换为PHP

时间:2011-11-10 01:12:24

标签: php javascript function

我正在尝试转换此功能:

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=')
 );
}

到php,这是我尝试过的:

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));
}

但它不起作用。有人能帮我吗? 感谢

1 个答案:

答案 0 :(得分:1)

您的str_replace()有错误。您必须替换*^|个字符,而不是\*\^\|\是正则表达式中的转义字符