6
投票
试试这个:
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 "
It is Palindrome
";}
else {
echo "
Not Palindrome";}
}
$string = "A man, a plan, a canal, Panama";
check_plaindrome($string);
########Output#######
It is Palindrome
0
投票
它可以这么简单吗?
function isPalindrome(string $word) : bool
{
$word = strtolower($word);
if(strrev($word) === $word){
return true;
} else {
return false;
}
}
echo isPalindrome('Deleveled');
-1
投票
要测试的虚拟字符串
$s = 'abcdefg';
反转字符串顺序
$t = strrev($s);
获取字符串的长度
$length = mb_strlen($s);
变量用于在使用for循环遍历时连接字符串
$new = '';
循环遍历字符串
for ($i = 0; $i < $length; $i++) {
$new = $s[$i].$new;
}
使用for循环遍历的检查字符串等于使用PHP函数反转的字符串
if ($s == $new){
echo "Yes palindrome";
} else {
echo "No palindrome";
}
如果输出为是,那么它是回文,否则它不是回文串。
***我已经更新了答案。
2
投票
我们可以这样写一个简单的方法,不要让它变得复杂:
$a = "1681";
$b = strrev($a);
print $b;
if($a == $b){
print "
Plaindrome";
} else {
print "
Not Plaindrome";
}
1
投票
没有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";
?>
1
投票
你可以尝试这个,它会反转字符串或值......
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;
}
}
0
投票
$word = "level"; // declare a varibale
echo "String: " . $word . "
";
$reverse = strrev($word); // reverse the word
if ($word == $reverse) // compare if the original word is same as the reverse of the same word
echo 'Output: This string is a palindrome';
else
echo 'Output: This is not a palindrome';
?>
0
投票
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;
}
0
投票
function checkPalindrome($string) {
return $string == strrev($string);
}
0
投票
您可以尝试以下代码:
function checkPalindrome($string){
$string = strtolower($string);
$reverse = strrev($string);
if($string == $reverse){
return true;
} else {
return false;
}
}
0
投票
试试这个......
$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";
}
?>