syntax highlight script- prob. with array within while loop
Posted: Sun Oct 20, 2002 10:26 pm
In short, I'm working on a highlighter script for my keyword search, and I've made a bunch of modifications recently, selecting to take the highlighter and make it part of the while loop that compiles the results. Here's the code:
The problem I am having is with this particular part:
The first part of the "if-else" control is fine. It works exactly like it should. What it does exactly is determine if the user has selected to do an exact search or not. Earlier in the script, it searches for double quotes in the search string and if they are present, strip them and the slashes, and assign "1" as the syntax variable. If the person is doing a regular search, it assigns "0" as the syntax variable, and this is where the problem comes up.
On a single word search, everything works fine. On a multiple word search however . . . Regardless of the number of words input, it will only highlight the last word.
It is supposed to take the regular search string, regardless of the amount of words input, explode it into an array, and for each word execute the highlighter then echo $ml.
I'm guessing this is something incredibly obvious involving the while loop and the foreach that's making it highlight the last word of the string only, but I honestly can't figure it out.
Thanks for any help in advance.
--Tony
Code: Select all
while ($r = mysql_fetch_array($result))
{
$title = $rї"title"];
$date = $rї"date"];
$vq = $rї"vq"];
$length = $rї"length"];
$format = $rї"format"];
if($syntax == 1) {
$ml = preg_replace("/\b($search*)\b/i","<span class="highlight">\\1</span>",$rї"ml"]);
} else {
$words = explode(' ', $search);
foreach( $words AS $word ) {
$ml = preg_replace("/\b($word*)\b/i","<span class="highlight">\\1</span>",$rї"ml"]); }
}
echo "<p class="bold" style="color: #186B08; font-size: 10pt;"> $title | $date | $vq | $length | $format </p>\n
<ol class="left">\n
$ml\n
</ol>\n\n";
}Code: Select all
else {
$words = explode(' ', $search);
foreach( $words AS $word ) {
$ml = preg_replace("/\b($word*)\b/i","<span class="highlight">\\1</span>",$rї"ml"]); }
}On a single word search, everything works fine. On a multiple word search however . . . Regardless of the number of words input, it will only highlight the last word.
It is supposed to take the regular search string, regardless of the amount of words input, explode it into an array, and for each word execute the highlighter then echo $ml.
I'm guessing this is something incredibly obvious involving the while loop and the foreach that's making it highlight the last word of the string only, but I honestly can't figure it out.
Thanks for any help in advance.
--Tony