dongluanan7163 2014-05-26 08:34
浏览 35
已采纳

PHP rot5 javascript等价吗?

I am wondering if there is a rot5 function for PHP equalivant too

function rot5(str) {
  var s = [];
  for (i = 0; i < str.length; i ++)
  {
    idx = str.charCodeAt(i);
    if ((idx >= 48) && (idx <= 57))
    {
      if (idx <= 52)
      {
        s[i] = String.fromCharCode(((idx + 5)));
      }
      else
      {
        s[i] = String.fromCharCode(((idx - 5)));
      }
    } 
    else
    {
      s[i] = String.fromCharCode(idx);
    }
  }
  return s.join('');
}

In javascript? If not then how can I do fromCharCode and charCodeAt in PHP?

  • 写回答

2条回答 默认 最新

  • dqppv86022 2014-06-05 14:32
    关注

    Here you got generic strrot with default to strrot5

    <?php
    
    function strrot($str, $n=5){
        $replace = [];
        for($i=0;$i<26;$i++){
            $replace[chr(65+$i)] = chr(65+(($i+$n)%26));
            $replace[chr(97+$i)] = chr(97+(($i+$n)%26));
        }
        return strtr($str, $replace);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部