【发布时间】:2020-02-04 13:44:54
【问题描述】:
我已经在 PHP 中整合了这个回文函数
<?php
// example code
function isPalindrome($str){
// eliminate special chars
$str = preg_replace('/[^a-z0-9]+/i', '', $str);
// all to lowercase
$str = strtolower($str);
// reverse string
$strArr = str_split($str);
$strArr = array_reverse($strArr);
$reversed_str = join('',$strArr);
//Test
//echo $str . ' | ' . $reversed_str;
// compare
return $str === $reversed_str;
}
echo isPalindrome("A nut for a jar of tuna"); // returns 1
问题:如果提供的字符串是回文,则函数返回1,否则返回nothing。
echo isPalindrome("A nut for a jar of fish"); // returns no output (in https://www.tehplayground.com)
我希望它返回真或假。我的错在哪里?
【问题讨论】:
标签: php