In article <[EMAIL PROTECTED]>, "Joseph H
Blythe" <[EMAIL PROTECTED]> wrote:
What is happening is that you are replacing the variable $replaced
on each iteration of the while loop. As a result, $replaced is only
storing the most recent match.
To see this, try this out:
$keywords = "foo";
$data = "This is a lower foo. This is an upper FOO. This is a ucase Foo";
$lower = strtolower($keywords);
$upper = strtoupper($keywords);
$ucase = ucwords($keywords);
$words = array($lower, $upper, $ucase);
while ( list($key, $val) = each($words) ) {
print("key: $key => val: $val<br>");
$hilite = "<font color=\"#ff0000\">" . $val . "</font>";
$replaced[] = ereg_replace($val, $hilite, $data);
}
for($i=0;$i<sizeof($replaced);$i++) {
echo $replaced[$i]."<br>";
}
The first print statement will show you what keywords we are
focusing on. The ereg_replace in this case is appending an arrary.
If you then walk that array, you will see that each of your keywords
was matched and highlited.
that should get you a little further along.
Jeff
> G'day PHP'ers
>
> I am trying highlight the results from a search, so far I have this
> working but only if the string is an exact match ie: case sensitive, for
> example:
>
> <?php
> $keywords = "foo";
> $data = "This is a lower foo. This is an upper FOO. This is a ucase
> Foo";
>
> $lower = strtolower($keywords);
> $upper = strtoupper($keywords);
> $ucase = ucwords($keywords);
> $words = array($lower, $upper, $ucase);
>
> while ( list($key, $val) = each($words) ) {
> $hilite = "<font color=\"#ff0000\">" . $val . "</font>";
> $replaced = ereg_replace($val, $hilite, $data);
> }
>
> echo $replaced;
> ?>
>
> Now as metioned this seems to work only for "foo" does anyone have any
> idea why?
>
> Also I need to be able to support mutiple keywords ie: foo Foo FOO, so
> basically I know I could split(" ", $keywords) then some how traverse
> the array making a new array of all possible case combinations then in
> my while loop replace each occurance found in $data with the highlighted
> replacment, sounds easy hmm....
>
> If anyone has any ideas or can see what I am doing wrong so far, it
> would be mutch appreciated.
>
> Regards,
>
> Joseph
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]