Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-06 20:09:19
Original
1432 people have browsed it

所谓回文字符串,兴许有很多人并不知晓是什么意思。其实呢,回文字符串就是从左往右看和从右往左看是一样的,就如同“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);   
?>  
Copy after login

你可以试试看看哦……

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!