我正在尝试创建一个PHP脚本,它将使用4个单词,并将所有字母更改为大写,并对字母进行洗牌。所有单词只能使用4-7个单词,没有数字。
现在,我的代码识别单词是否符合4-7限制,如果单词包含数字,则表示错误。
我有两个问题:
我不知道出了什么问题。任何建议:
HTML
<form action="process_JumbleMaker.php" method="post">
Word 1: <input type="text" name="Word1" /><br />
Word 2: <input type="text" name="Word2" /><br />
Word 3: <input type="text" name="Word3" /><br />
Word 4: <input type="text" name="Word4" /><br />
<input type="reset" value="Clear Form" />
<input type="submit" name="Submit" value="Send Form" />
</form>
</body>
PHP
<?php
function displayError($fieldName, $errorMsg) {
global $errorCount;
echo "Error for \"$fieldName\": $errorMsg \n";
++$errorCount;
}
function validateWord($data, $fieldName) {
global $errorCount;
if (empty($data)) {
displayError($fieldName,"This field is required");
$retval = "";
} else {
$retval = trim($data);
$retval = stripslashes($retval);
if ((strlen($retval)<4) || (strlen($retval)>7)) {
displayError($fieldName,"Words must be at least four and at most seven letters long");
}
if (preg_match("/^[a-z]+$/i",$retval)==0) {
displayError($fieldName,"Words must be only letters");
}
}
$retval = strtoupper($retval);
$retval = str_shuffle($retval);
return($retval);
}
$errorCount = 0;
$words = array();
$words[] = validateWord($_POST['Word1'], "Word 1");
$words[] = validateWord($_POST['Word2'], "Word 2");
$words[] = validateWord($_POST['Word3'], "Word 3");
$words[] = validateWord($_POST['Word4'], "Word 4");
if ($errorCount>0) {
echo "Please use the \"Back\" button to re-enter the data. \n";
}
else {
$wordnum = 0;
foreach ($words as $word)
echo "Word ".++$wordnum.": $word\n";
}
?>
发布于 2021-04-11 02:43:40
您有str_shuffle
函数调用的拼写错误,f
和l
已组合成一个连接,而不是单个字母,因此更改函数调用将修复您的错误。
$retval = str_shuffle($retval); // Right
$retval = str_shuffle($retval); // Wrong
https://stackoverflow.com/questions/67040520
复制相似问题