Php 回文函数存在逻辑错误
我用PHP编写了一个函数来检查一个单词/短语是否是回文,但它不起作用 我认为其中有一些错误Php 回文函数存在逻辑错误,php,Php,我用PHP编写了一个函数来检查一个单词/短语是否是回文,但它不起作用 我认为其中有一些错误 <?php if(isset($_GET["text_string"])){ //print("GET is set.<br />"); //test whether GET works $inputText = $_GET["text_string"]; // print("inputText value is: "
<?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 />");
?>
改为
if ($inputText != "") {
同时将此错误消息置于else状态
else {
print("GET did not work.<br />");
}
else{
打印(“GET不起作用。
”);
}
改为
if ($inputText != "") {
同时将此错误消息置于else状态
else {
print("GET did not work.<br />");
}
else{
打印(“GET不起作用。
”);
}
这是否适合您的需要
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);
函数isAlindrome($s){
//条状穿孔
$str=preg_replace('/[^\w\s]/',''$s);
//带状空白
$str=爆炸(“”,$str);
$str=内爆(“”,$str);
//强制小写
$str=strtolower($str);
//逆转
$rts=STREV($str);
//比较并显示消息
$result=$str==$rts?“这是回文”:(“;
回显$result。“
”;
}
$phrase1=‘先生,红色朗姆酒是谋杀?’;
isPalindrome($phrase1);
$phrase2='红色朗姆酒,先生,这是谋杀';
isPalindrome($phrase2);
这是否适合您的需要
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);
函数isAlindrome($s){
//条状穿孔
$str=preg_replace('/[^\w\s]/',''$s);
//带状空白
$str=爆炸(“”,$str);
$str=内爆(“”,$str);
//强制小写
$str=strtolower($str);
//逆转
$rts=STREV($str);
//比较并显示消息
$result=$str==$rts?“这是回文”:(“;
回显$result。“
”;
}
$phrase1=‘先生,红色朗姆酒是谋杀?’;
isPalindrome($phrase1);
$phrase2='红色朗姆酒,先生,这是谋杀';
isPalindrome($phrase2);
您的错误是什么?这个脚本应该如何工作?屏幕上没有错误,但没有给出输出,但在url上get正在工作如果($inputText==“”){意味着你的代码只有在不存在的情况下才能工作。这是一个错误。如果你的PHP在Apache内部运行,请查看错误日志,查看每次加载页面时出现的消息。你的错误是什么,这个脚本应该如何工作?屏幕上没有错误,但没有给出输出,但url上get正在工作($inputText==“”){意味着你的代码只有在不存在的情况下才能工作。这是一个错误。如果你的PHP在Apache内部运行,请查看错误日志,查看每次加载页面时出现的消息。