Forum Moderators: coopster

Message Too Old, No Replies

an array of search results

         

wizpl

11:29 pm on Sep 27, 2006 (gmt 0)

10+ Year Member



I try to write a simple script that would

- 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?

Psychopsia

11:42 pm on Sep 27, 2006 (gmt 0)

10+ Year Member



Hi

Yes, make sense for me.

$article and $newtext are dinamic, right?

wizpl

11:56 pm on Sep 27, 2006 (gmt 0)

10+ Year Member



$article will always have the same three elements.

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

wizpl

3:12 pm on Sep 28, 2006 (gmt 0)

10+ Year Member



...so anyone can give me a hint how to approach this and which function to use?

thanks for help

coopster

5:55 pm on Sep 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would use preg_replace_callback() [php.net] with a static [php.net] variable for the array index and the matched pattern for the value. I would also initialize the array in the global [php.net] scope first.
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;

WebmasterWorld forums break the pipe symbol, so if you copy/paste don't forget to rekey the pipes.

wizpl

6:48 am on Sep 29, 2006 (gmt 0)

10+ Year Member



coopster, thanks a lot. This gave me a great start and I've learned a lot trying to understand your code and develop it further.


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>
";
}