php Php程序寻找回文?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33932065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:40:06  来源:igfitidea点击:

Php Program to find Palindrome?

php

提问by sudhakar

Three steps to find the palindrome?

找到回文的三个步骤?

  1. use strrevon the given value

  2. Then split using the str_split.

  3. Then use foreachand concatethe split value.

  1. 使用strrev给定的值

  2. 然后使用str_split.

  3. 然后使用foreachconcate分割值。

Example

例子

$a = "madam";

$b =  strrev($a);

    $string_reverse = str_split($b);

    $palin = '';

    foreach($string_reverse as $value){

        $palin.= $value; 
    }

    print $palin;

    if($a == $palin){

        print "<br>Palindrome";

    } else {

        print "<br>Not Palindrome"; 

    }

Output

输出

madam
Palindrome


回文女士

回答by FelixHo

try this:

尝试这个:

<?php
function check_plaindrome($string) {
    //remove all spaces
    $string = str_replace(' ', '', $string);

    //remove special characters
    $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string);

    //change case to lower
    $string = strtolower($string);

    //reverse the string
    $reverse = strrev($string);

    if ($string == $reverse) {
        echo "<p>It is Palindrome</p>";
    } 
    else {
        echo "</p>Not Palindrome</p>";
    }
}

$string = "A man, a plan, a canal, Panama";
check_plaindrome($string);


########Output#######
<p>It is Palindrome</p>

回答by user7740335

There is a simple way who we can write like this to don't make it complicate:

有一个简单的方法,我们可以这样写,不要让它变得复杂:

$a = "1681";

$b =  strrev($a);

print $b;

if($a == $b){

    print "<br>Plaindrome";

} else {

    print "<br>Not Plaindrome"; 

}

回答by Muhammad Shahzad

To check a string whether it is palindrome or not without PHP function.

不用PHP函数检查一个字符串是否为回文。

<?php 

    $str = 'level';
    $strLen = strlen($str)-1;
    $revStr = '';
    for($i=$strLen; $i>=0; $i--){
        $revStr.=$str[$i];
    }
    if($revStr == $str)
        echo 'Palindrome';
    else
        echo "Not Palindrome";

?>

回答by Tiberiu

function checkPalindrome($string) {
    return $string == strrev($string);
}

回答by Chigozie Orunta

You can try this, it reverses the string or value...

你可以试试这个,它反转字符串或值......

function fn_palindrome($palindrome) {
    $reversed = ''; 
    $original = $palindrome;
    $string = array(); $j = 0;
    $converted = (string) $palindrome;
    $palindrome = str_split($converted);
    $i = count($palindrome) - 1;
    while($i >= 0) {
        $string[$j] = $palindrome[$i];
        $j++; $i--;
    }
    $reversed = implode('', $string);
    if($reversed == $original) {
        return TRUE;
    } else {
        return FALSE;
    }
}

回答by chohan

Dummy String to be tested

待测试的虚拟字符串

$s = 'abcdefg';

Reverse the string order

反转字符串顺序

$t = strrev($s);

Get the length of the string

获取字符串的长度

$length = mb_strlen($s);

Variable to be used for concating string while traversing using for loop

使用 for 循环遍历时用于连接字符串的变量

$new = '';

Loop to traverse string

循环遍历字符串

for ($i = 0; $i < $length; $i++) {
    $new = $s[$i].$new;    
}

Check string traversed using for loop is equal to string reversed using PHP function

检查使用for循环遍历的字符串是否等于使用PHP函数反转的字符串

if ($s == $new){
    echo "Yes palindrome";
} else {
    echo "No palindrome";
}

If output is yes then it is palindrome otherwise it is Not a palindrome string.

如果输出为是,则它是回文,否则它不是回文字符串。

*** I have update the answer.

*** 我已经更新了答案。

回答by Mohamed Sabr

You can try the following code:

您可以尝试以下代码:

function checkPalindrome($string){
   $string = strtolower($string);
   $reverse = strrev($string);
       if($string == $reverse){
          return true;
       } else {
          return false;
       }
}

回答by Ganesh Aher

Try this one..

试试这个..

<?php
  $strng = 'SMS';
  $rvsstr = '';
  $i = 0;
  while (!empty($strng[$i])) {
    $rvsstr = $strng[$i].$rvsstr;
    $i++;
  }
  // echo $rvsstr;

  if ($strng === $rvsstr) {
    echo "Number is Palindrome";
  } else {
    echo "Number is not palindrome";
  }
?>

回答by m33bo

Could it be as simple as this?

能这么简单吗?

function isPalindrome(string $word) : bool
{
  $word = strtolower($word);
  if(strrev($word) === $word){
    return true;
  } else {
    return false;
  }
}   

echo isPalindrome('Deleveled');

回答by john ktejik

    function checkpala(string $input){

    for( $i=0; $i < strlen($input); $i++){   //for each char in the string
        if (substr($input,$i,1) != substr($input, strlen($input)-($i+1),1)){  //get the first char and the last char and compare them.  Then get the 2nd and the 2nd from the end and compare them.  repeate

            //if at any point there is no match, stop checking, return false
            //echo "$input is not a palindrome";
            return false;
         }
    }
    //if the loop completes and every character checked out, return true.
    //echo "$input is a palindrome";
    return true;
 }