Home  >  Article  >  php教程  >  PHP:判断一个字符串是否为回文字符串

PHP:判断一个字符串是否为回文字符串

WBOY
WBOYOriginal
2016-06-06 20:09:191433browse

所谓回文字符串,兴许有很多人并不知晓是什么意思。其实呢,回文字符串就是从左往右看和从右往左看是一样的,就如同“CDDC”这样的例子。这么说,不知道大家明白没有。废话呢,也就不多说了,直接贴出代码。具体的代码如下: ?php function ishuiwen($str){

所谓回文字符串,兴许有很多人并不知晓是什么意思。其实呢,回文字符串就是从左往右看和从右往左看是一样的,就如同“CDDC”这样的例子。这么说,不知道大家明白没有。废话呢,也就不多说了,直接贴出代码。具体的代码如下:

<?php function ishuiwen($str){   
   $len=strlen($str);   
   $l=1;   
   $k=intval($len/2)+1;   
     for($j=0;$j<$k;$j++){   
       if (substr($str,$j,1)!=substr($str,$len-$j-1,1))   
          {   
   $l=0;   
   break;   
     }   
         }   
  if ($l==1)  {   
  return 1;     
}  else  {    
 return -1;      
 }     
}       
$str=12321;    
echo ishuiwen($str);   
?>  

你可以试试看看哦……

转载请注明:IT路人 » PHP:判断一个字符串是否为回文字符串

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn