首页  >  文章  >  后端开发  >  PHP之判断输入的字符串是不是回文

PHP之判断输入的字符串是不是回文

WBOY
WBOY原创
2016-07-28 08:26:101294浏览

回文:正序和倒序完全相同

<code><span>$string</span>=<span>"abc126321cba"</span>;
  <span>$stra</span>=<span>array</span>();
  <span>$strb</span>=<span>array</span>();
  <span>$res</span>=checkhuiwen(<span>$string</span>);
  <span><span>function</span><span>checkhuiwen</span><span>(<span>$string</span>)</span>{</span><span>//输入字符串转换为数组</span><span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span><strlen>$string);<span>$i</span>++){
        <span>$stra</span>[<span>$i</span>]=<span>$string</span>[<span>$i</span>];
      }
       <span>//逆序输出数组array_reverse</span><span>$strb</span>=array_reverse(<span>$stra</span>);
       <span>//判断两数组是否相等,相等为回文</span><span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span><count>$stra);<span>$i</span>++){
       <span>if</span>(<span>$stra</span>[<span>$i</span>]!=<span>$strb</span>[<span>$i</span>]){
          <span>return</span><span>1</span>;
          <span>break</span>;
       }     
  }
  <span>return</span><span>2</span>;
  }
  <span>if</span>(<span>$res</span>==<span>2</span>){
    <span>echo</span><span>"YES"</span>;
  }<span>else</span>{
    <span>echo</span><span>"NO!"</span>;
  }</count></strlen></code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了 PHP之判断输入的字符串是不是回文,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn