Forum Moderators: coopster
- take a string of text.
- find any occurrence of " a ", " an " or " the "
- replace each occurance with a consecutive count number
- create an array where each key contains the respective string (a, an, the)
e.g.:
$text = "A fraction of a report by United States intelligence agencies on the global terrorist threat.";
$article = array(" a "," an "," the ")
would result in:
$newtext = "#1 fraction of #2 report by United States intelligence agencies on #3 global terrorist threat."
Array
(
[1] => A
[2] => a
[3] => the
)
does this explanation make sense?
The $text string will come from a form input.
and after processing it should result in $newtext string and an array listing replaced words.
This script is supposed to turn any text into an English grammar test for indefinite and definite articles (a, an, the). Replacing all articles with form fields and then checking the answers, comparing the form input to an array.
...so the $newtext is a processed version of the $text
function numberArticle($matches)
{
static $n = 0;
$GLOBALS['articlesFound'][$n++] = $matches[0];
return "#$n";
}
$article = array('a','an','the');
// An easy way to build our pattern,
// $pattern = '/\b(a¦an¦the)\b/i';
$pattern = '/\b(' . implode('¦', $article) . ')\b/i';
$articlesFound = array();
print preg_replace_callback($pattern, 'numberArticle', $text) . "\n";
exit;
if ($_POST['textclip']) {
$text = $_POST['textclip'];
function prepare($match)
{
static $n = 0;
$_SESSION['articlesFound'][$n++] = $match[0];
$replace = "<input name='$n' type='text' size='1'>";
return "$replace";
}
$pattern = '/\b(aŠanŠthe)\b/i';
$exercise = preg_replace_callback($pattern, "prepare", $text) . "\n";
$page_top = "<h1>Grammar Test: Articles</h1>
<form action='$form' method='post'>
<p class='justify'>$exercise<br><br></p>
<p><input type='submit' name='request' value='check' class='button_off' onmouseover=\"className='button_on';\"
onmouseout=\"className='button_off';\">
<input type=\"hidden\" value=\"$text\" name=\"test\"></p>
</form>
" ;
} elseif ($_POST['test']) {
$text = $_POST['test'];
global $good, $bad;
$good = 0;
$bad = 0;
function check($match) {
static $n = 1;
if ($match[0] == $_POST[$n]) {
$replace = "$match[0]";
++$GLOBALS['good'];
} elseif ($_POST[$n] == "") {
$replace = "<strong class='orange'>x</strong>";
++$GLOBALS['bad'];
} else {
$replace = "<strong class='orange'>$_POST[$n]</strong>";
++$GLOBALS['bad'];
}
$n++;
return "$replace";
}
$pattern = '/\b(aŠanŠthe)\b/i';
$tested = preg_replace_callback($pattern, "check", $text) . "\n";
$score = round((100*$good)/($good+$bad));
if ($bad == 0) { $message = "Excellent!"; }
elseif ($bad == 1) { $message = "1 error!"; }
else { $message = "$bad errors!"; }
$page_top = "<h1>$message Your score is $score%</h1>
<p class='justify'>$tested<br><br></p>
<form action='$form' method='post'>
<p><input type='submit' name='request' value='new' class='button_off' onmouseover=\"className='button_on';\"
onmouseout=\"className='button_off';\"></p>
</form>
";
} else {
$page_top = "<h1>Grammar Test: Articles</h1>
<form action='$form' method='post'>
<p>Copy some text here<br>
<textarea name='textclip' rows='8' cols='32'></textarea><br><br></p>
<p><input type='submit' name='request' value='enter' class='button_off' onmouseover=\"className='button_on';\"
onmouseout=\"className='button_off';\"></p>
</form>
";
}