Stupid php/brain failing me

Soldato
Joined
24 Nov 2002
Posts
16,378
Location
38.744281°N 104.846806°W
I've written the following to highlight search terms:

Code:
function highlight($text, $t, $q){

if ($q == "and") {
$array = explode(", ", $t);
	foreach ($array as &$value){
	$text = str_replace(strtolower($value), "<span class=\"highlight\">" . strtolower($value) . "</span>", $text);
	$text = str_replace(strtoupper($value), "<span class=\"highlight\">" . strtoupper($value) . "</span>", $text);
	}
***	return $text;
	
}else{
		$text = str_replace(strtolower($t), "<span class=\"highlight\">" . strtolower($t) . "</span>", $text);
		$text = str_replace(strtoupper($t), "<span class=\"highlight\">" . strtoupper($t) . "</span>", $text);
		return $text;
}
}

$t is the search term, now this can be a comma seperated list (if q = and it is csv), hence the explode. However, only the first 'exploded' item is highlighted.

I'm too tired to spot my mistake, but I think it's by ***.

Any ideas?
 
Soldato
OP
Joined
24 Nov 2002
Posts
16,378
Location
38.744281°N 104.846806°W
Well, it works actually.

However, it'll only work for the first row of results, not the second.

Code:
$result = mysql_query($sql) or die('Error, query failed');
$num =  mysql_num_rows($result);
echo "results found: " . $num;
echo "<br><hr><br>";
while($rows = mysql_fetch_array( $result )) 
{
echo "<b>gc</b><br>";
echo [B]highlight($rows['gc'], $t, $q)[/B];

I've no idea why it won't work for the subsequent rows.

EDIT

furnace said:
dont you want $array = explode(", ", $t) to be exploding the $text rather than the search term?

No, I want to explode the search term, as it could be "cats, dogs"...
 
Soldato
Joined
12 Dec 2006
Posts
3,421
Location
Worthing, West Sussex
Your str_replace's strlower and strupper...

By doing it that way, you cant search for "Cat" - only "CAT" or "cat"..

Does this make any difference?

Code:
function highlight($text, $term, $isCSV=false){

	$output=strtolower($text);
	$term=strtolower($term);
	
	if($isCSV){
		
		$words = explode(", ",$term);
		
		foreach ($words as $wordNum => $word){
		
			$output=str_replace($word, "<span class=\"highlight\">".$word."</span>", $output);
		
		}
		
	}else{
	
		$output=str_replace($term, "<span class=\"highlight\">".$term."</span>", $output);
	
	}

	return $output;

}

I'm tired too, too lazy to test that so sorry if it just errors or something :o

edit; apologies if I've misunderstood again, I really can't think straight right now
 
Last edited:
Soldato
OP
Joined
24 Nov 2002
Posts
16,378
Location
38.744281°N 104.846806°W
furnace said:
lol, cant complain though! :p

Sorry I was too lazy to test before posting ;) But glad it helped
I managed to take out the 'if' alltogether...

Apparently if you try and explode with a character not in the string it'll keep it as a string - handy!

Cheers for your help.. I guess making contingency plans (the if statement) wasn't needed... :)
 
Soldato
OP
Joined
24 Nov 2002
Posts
16,378
Location
38.744281°N 104.846806°W
furnace said:
but what if you want to search for something with a comma in it? it'd explode it, but it might only be one term? if you have an if you can explicitly say "this is one term" :) just food for thought!
Alright smarty pants, I sorted it.

I sitll have no idea why $q didn't work though, in the end I called the var directly ($_POST["query"]).

Cheers for all your help.
 
Associate
Joined
5 Mar 2007
Posts
649
Location
London
Code:
<?php

$term = someSanitiseFunc($_GET['someTerm']);
define('TERM', $term);

function highlight($arr)
{
	return str_replace(TERM, '<span class="highlight">' . TERM . '</span>', $arr);
}

while($row = mysql_fetch_array($res))
{
	$highlighted = array_map('highlight', $row);
	// Yada yada
}

?>
Just another method—a lot cleaner IMO—which you can work case sensitivity and CSV support into.
 
Soldato
OP
Joined
24 Nov 2002
Posts
16,378
Location
38.744281°N 104.846806°W
psyr33 said:
Just another method—a lot cleaner IMO—which you can work case sensitivity and CSV support into.
Thanks.

I think I'll just stick with what I have for now, though:
Code:
function highlight($text, $t){
if (anti_sql_injct($_POST["query"]) == "and") {
$array = explode(", ", $t);
	foreach ($array as &$value){
	$text = str_replace(strtolower($value), "<span class=\"highlight\">" . strtolower($value) . "</span>", $text);
	$text = str_replace(strtoupper($value), "<span class=\"highlight\">" . strtoupper($value) . "</span>", $text);
	$text = str_replace(ucfirst(strtolower($value)), "<span class=\"highlight\">" . ucfirst(strtolower($value)) . "</span>", $text);
	}
}else{
$value = $t;
	$text = str_replace(strtolower($value), "<span class=\"highlight\">" . strtolower($value) . "</span>", $text);
	$text = str_replace(strtoupper($value), "<span class=\"highlight\">" . strtoupper($value) . "</span>", $text);
	$text = str_replace(ucfirst(strtolower($value)), "<span class=\"highlight\">" . ucfirst(strtolower($value)) . "</span>", $text);
}
return $text;
}

Ahhh.... long-winded... :)
 
Soldato
OP
Joined
24 Nov 2002
Posts
16,378
Location
38.744281°N 104.846806°W
jdickerson said:
Fixed.

Mwahhahaha :D
Sadly, not really fixed! :(

Replaced everything with:

Code:
function highlight($text, $term){
$term = str_replace(",", "", $term);
$exploded_terms = explode(" ", $term);

	foreach ($exploded_terms as &$singleterm){
    $singleterm = '(.*)('.$singleterm.')(.*)';
    $text = eregi_replace($singleterm, '\1<span class="highlight">\2</span>\3', $text);
	}
	
return $text;
}
But, for some reason if the search terms were "egg bacon beans chicken", not all of the terms will be highlighted. Well, some of each will be highlighted, but not all.

This can be avoided by replacing (.*) with (.?)..... but that will cause a different error....

Any pointers?
 
Back
Top Bottom