Re: ereg_replace all items in a array
| From: | Jeff Warrington | Date: | Tue, 30 Jan 2001 01:42:52 +0000 |
| Subject: | Re: ereg_replace all items in a array | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-37206@lists.php.net to get a copy of this message | ||
In article <20010130013332.51e2c283.joe@binarylogic.com.au>, "Joseph H
Blythe" <joe@binarylogic.com.au> 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
>
>