所以最近我正在研究PHP,我设法设置了一个小测验,利用它。我的问题是如何让测验接受略有拼写的错误答案?例如,如果我的测验询问阿拉巴马州的首都是什么,而且蒙哥马利的拼写有些错误,我怎么能让它仍然算得上呢?我想对我的测验提出的所有问题都这样做,我希望我已经把我的问题措辞得足够好,如果不是让我知道的话。这是我目前正在使用的代码,是的,它被设置为三个文件,因为我发现它在一个文件中很复杂,我希望答案指的是我有的多个文件,我不想要一个例子如果可能的话,在一个页面上的PHP代码。代码:
列出州和首都的代码,不是完整列表,而是示例:
<?php
$states = Array();
$capitals = Array();
$states[]="Alabama";
$capitals[]="Montgomery";
$states[]="Alaska";
$capitals[]="Juneau";
$states[]="Arizona";
$capitals[]="Phoenix";
$states[]="Arkansas";
$capitals[]="Little Rock";
$states[]="California";
$capitals[]="Sacramento";
$states[]="Colorado";
$capitals[]="Denver";
$states[]="Connecticut";
$capitals[]="Hartford";
$states[]="Delaware";
....etc
?>
测验基础的实际主要代码:
<!DOCTYPE html>
<html>
<head>
<title>State capital quiz: ask</title>
</head>
<body>
<h1>State Capital Quiz </h1><p>
<?php
$saywhich=@$_GET['saywhich'];
if ($saywhich){
include("statecapitals.php");
$which=$_GET['which'];
$choice=rand(0, sizeOf($states)-1);
if ($which=='state') {
$state = $states[$choice];
print("What is the capital of $state?<br>");
print("<form action='statecapquizcheck.php' method='get'>\n");
print("<input type='text' name='capital'><br>\n");
print("<input type='hidden' name='which' value=$which>\n");
print("<input type='hidden' name='choice' value=$choice>\n");
print("<input type='submit' value='Submit Answer'>");
print("</form>\n");
}
else {
$capital = $capitals[$choice];
print("$capital is the capital of which state?<br>");
print("<form action='statecapquizcheck.php' method='get'>\n");
print("<input type='text' name='state'><br>\n");
print("<input type='hidden' name='which' value=$which>\n");
print("<input type='hidden' name='choice' value=$choice>\n");
print("<input type='submit' value='Submit Answer'>");
print("</form>\n");
}
}
else {
print("Choose form of question: do you want to be given the state or the capital?<br>");
print("<form action='statecapquizask.php' method='get'>\n");
print("Ask <input type='radio' name='which' value='state'>State");
print(" <input type='radio' name='which' value='capital'>Capital\n");
print("<input type='hidden' name='saywhich' value='true'>\n");
print("<input type='submit' value='Submit choice'>");
print("</form>");
}
?>
</body>
</html>
检查答案是否正确的代码:
!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>State capitals quiz: check</title>
</head>
<body>
<h1>State Capital Quiz </h1><p>
<?php
include('statecapitals.php');
$choice=$_GET['choice'];
$state=@$_GET['state'];
$capital=@$_GET['capital'];
$which=$_GET['which'];
$correctstate=$states[$choice];
$correctcapital=$capitals[$choice];
if ($which=='state') {
if ($capital == $correctcapital) {
print("Correct! $correctcapital is the capital of $correctstate!");
print("<p><a href='statecapquizask.php'>Play again </a>");
}
else {
print("WRONG!<p>\n");
print("<a href='statecapquizask.php'>New turn </a><p>\n");
print("OR try again: What is the capital of $correctstate?<br>");
print("<form action='statecapquizcheck.php' method='get'>\n");
print("<input type='text' name='capital'><br>\n");
print("<input type='hidden' name='state' value=$state>\n");
print("<input type='hidden' name='which' value=$which>\n");
print("<input type='hidden' name='choice' value=$choice>\n");
print("<input type='submit' value='Submit Answer'>");
print("</form>\n");
} }
else {
if ($state == $correctstate) {
print("Correct! The capital of $correctstate is $correctcapital!");
$saywhich='false';
print("<p><a href='statecapquizask.php'>Play again </a>");
}
else {
print("WRONG!<p>\n");
print("<a href='statecapquizask.php'>New turn </a><p>\n");
print("OR try again: $correctcapital is the capital of what state?<br>");
print("<form action='statecapquizcheck.php' method='get'>\n");
print("<input type='text' name='state'><br>\n");
print("<input type='hidden' name='capital' value=$capital>\n");
print("<input type='hidden' name='which' value=$which>\n");
print("<input type='hidden' name='choice' value=$choice>\n");
print("<input type='submit' value='Submit Answer'>");
print("</form>\n");
} }
?>
</body>
</html>
答案 0 :(得分:2)
我希望你有足够的时间自己玩这个,因为这是你学习的方式。
如果您已经花时间自行破解,那么我会提交此脚本,以便您从中收集任何内容。我拍摄了你的场景并编写了一个程序脚本来试一试;我对soundex()
感到好奇。是的,这是一个单页...不是我通常会做的事情,因为我会使用类,以便它是单元可测试的。
这在顶部使用PHP的约定; HTML底部。这有助于强制逻辑和表示的分离。
html可能有效,也可能无效;它是未经测试的。
FWIW,我发现soundex
非常,非常,非常 原谅!相反,我使用了metaphone
。
<?php
// initialize all variables
$get = $_GET; // this was done for testing and I'm too lazy to replace it ;)
$success = false;
$choose_which = true;
$supplied_term = '';
$which = '';
$checked_answer = false;
$answer_array = array();
// state => capital
$states = array(
'Alabama' => 'Montgomery',
'Alaska' => 'Juneau',
'Arizona' => 'Phoenix',
'Arkansas' => 'Little Rock',
'California' => 'Sacremento',
// ... etc
);
// capital => state
$capitals = array_flip($states);
function get_random($stateOrCapital) {
$indexed_list = array_keys($stateOrCapital);
$random_index = (rand(0, sizeOf($indexed_list)-1));
return $indexed_list[$random_index];
}
function check_answer($submitted,$correct) {
//return (soundex($submitted) == soundex($correct)); // too forgiving
return (metaphone($submitted) == metaphone($correct));
}
// are we checking state or capital? Decouple GET input, choose which list to use for answers
if( array_key_exists('which', $get)) {
// don't show radio buttons
$choose_which = false;
switch($get['which']) {
case 'capital':
$which = 'capital';
$answer_array = $capitals;
break;
default:
$which = 'state';
$answer_array = $states;
}
}
// show question?
if($choose_which == false) {
$supplied_term = get_random($answer_array);
}
// check answer?
if( array_key_exists('check_answer', $get)) {
$supplied_term = htmlentities($get['supplied_term']);
$correct_answer = $answer_array[$supplied_term];
$success = check_answer($get['answer'],$correct_answer);
$checked_answer = true;
}
// default is to ask which type of question
// everything below could be separated into a view.
?><html>
<head>
<title>State capital quiz</title>
</head>
<body>
<h1>State Capital Quiz </h1>
<?php if($checked_answer): ?>
<?php if($success): ?>
<h2>CORRECT</h2>
<?php else: ?>
<h2>Sorry, try again...</h2>
<?php endif; ?>
<?php endif; ?>
<?php if($choose_which || $success): ?>
<h2>Which do you wish to answer?</h2>
<form action='' method="get">
<div><label>Capital <input type='radio' name='which' value='capital' /></label></div>
<div><label>State <input type='radio' name='which' value='state' /></label></div>
<div><input type='submit' /></div>
</form>
<?php else: ?>
<p>What is the matching state or capital for <?= $supplied_term ?></p>
<form action='' method='get'>
<input type='hidden' name='check_answer' value='true'>
<input type='hidden' name='which' value='<?= $which ?>'>
<input type='hidden' name='supplied_term' value='<?= $supplied_term ?>'>
<input type='text' name='answer'><br>
<input type='submit' value='Submit Answer'>
</form>
<?php endif; ?>