syntax highlight script- prob. with array within while loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TonySWcom
Forum Newbie
Posts: 6
Joined: Mon Sep 23, 2002 8:41 am
Contact:

syntax highlight script- prob. with array within while loop

Post by TonySWcom »

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:

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&#1111;"ml"]);
&#125; else &#123;
$words = explode(' ', $search);
foreach( $words AS $word ) &#123;
$ml = preg_replace("/\b($word*)\b/i","<span class="highlight">\\1</span>",$r&#1111;"ml"]); &#125;
&#125;

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

&#125;
The problem I am having is with this particular part:

Code: Select all

else &#123;
$words = explode(' ', $search);
foreach( $words AS $word ) &#123;
$ml = preg_replace("/\b($word*)\b/i","<span class="highlight">\\1</span>",$r&#1111;"ml"]); &#125;
&#125;
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
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

can't say that i can find anything wrong (probably cause im tired) but you may want to give this a try:
using explode and implode transform your $search variable from a space delimited string to a pipe delimited string ("word1 word2 word3" becomes "word1|word2|word3"). once you have this you can use your preg_replace statement like this:

Code: Select all

//$search is now pipe delimited
$ml = preg_replace("/\b($search*)\b/i","<span class="highlight">\\1</span>",$r&#1111;"ml"]);
what happens is that the pipes act as OR statements in the regex expression.
hope this helps.
TonySWcom
Forum Newbie
Posts: 6
Joined: Mon Sep 23, 2002 8:41 am
Contact:

Post by TonySWcom »

With a couple of slight modifications, your idea was in fact the one I was looking for.

I dropped the foreach loop, which seems to have been pointless to begin with, and went with this:

Code: Select all

$words = explode(' ', $search);
$word = implode('|', $words);
$ml = preg_replace("/\b($word*)\b/i","<span class="highlight">\\1</span>",$r&#1111;"ml"]);
It works beautifully. Thank you.

Now, I'm gonna add one final minor function to the highlighter that should be no problem and leave it, because it's probably as fast as it's ever going to get.

That function, BTW, is use of multiple colors in the highlighter, as determined by the amount of words input. Shouldn't be even a slight problem.

Again, thank you.

--Tony
Post Reply