我在PHP中编写了一个函数,用于检查单词/短语是否是回文,但是它不起作用。
我认为它有一些错误。
<?php
if(isset($_GET["text_string"])){
//print("GET is set.<br />"); //test whether GET works
$inputText = $_GET["text_string"];
// print("inputText value is: ".$inputText."<br />"); //test value of $inputText
$inputText = stripslashes(trim($inputText));
if($inputText == "") {
$forwardText = $inputText;
$reverseText = strrev($inputText);
if(strcmp($forwardText, $reverseText) == 0){
echo "<p class='yes'>The text you entered: <strong>'",$inputText, "'</strong> is a perfect palindrome!</p>";
}else{
echo "<p class='no'>The text you entered: <strong>'", $inputText, "'</strong> is NOT a perfect palindrome.</p>";
}
}else{
echo "<p class='wrong'>Enter a word or phrase and click the 'Check for Perfect Palindrome' button.</p>";
}
}
print("GET did not work.<br />");
?>
答案 0 :(得分:2)
if ($inputText == "") {
更改为
if ($inputText != "") {
还将此错误消息置于其他情况下。
else {
print("GET did not work.<br />");
}
答案 1 :(得分:0)
这是否符合您的需求?
function isPalindrome($s){
// Strip punctuaion
$str = preg_replace('/[^\w\s]/', '', $s);
// Strip white space
$str = explode(' ', $str);
$str = implode('', $str);
// Force lowercase
$str = strtolower($str);
// Reverse
$rts = strrev($str);
// Compare and show message
$result = $str == $rts ? "It's a palindrome" : ":(" ;
echo $result . '<br>';
}
$phrase1 = 'Red rum, sir, is murder?';
isPalindrome($phrase1);
$phrase2 = 'Red rum, sir, it is murder.';
isPalindrome($phrase2);