Page 1 of 1

Replacing keywords with highlighted keywords.

Posted: Wed Nov 23, 2005 11:58 am
by Luke
I am making a search, and I need a function to scan through the results, and replace keywords with highlighted keywords. The one I have is a pathetic attempt. I think I asked this question a long time ago, but never came back to see the response. HEre's my function.

Code: Select all

function highlight($row, $keywords){
		foreach($row as $key => $val){
			$highlight = "<font style=\"color: red; font-weight: bold\">$keywords</font>";
			if($key != $this->dirtable."_email" && $key != $this->dirtable."_website"){
				$row[$key] = str_ireplace($keywords, $highlight, $val);
			}
		}
		return $row;
	}
I would prefer something that replaces the keywords and preserves their case (this one doesn't) and this function doesn't replace multiple keywords.

Posted: Wed Nov 23, 2005 12:32 pm
by wtf

Code: Select all

str_replace($keyword, "<span style=\"background-color: #FFFF00\">$keyword</span>", $result['column'])

Re: Replacing keywords with highlighted keywords.

Posted: Wed Nov 23, 2005 12:32 pm
by hawleyjr
The Ninja Space Goat wrote:I think I asked this question a long time ago, but never came back to see the response. HEre's my function.

Huh???

viewtopic.php?t=40734&highlight=highlight

Posted: Wed Nov 23, 2005 1:55 pm
by Luke
I am completely lost... how would I do this? I don't know regex very well. Anybody know regex feel like making a function or at lease pointing me in the right direction?

Posted: Wed Nov 23, 2005 2:02 pm
by hawleyjr
Check out the link I posted. Feyd provides a working function there...

Posted: Wed Nov 23, 2005 3:30 pm
by Luke
I tried it right after you posted it... thanks, but I can't get it to work. I may be doing something wrong. If you look at it, there is a function called createfunction() that I don't really understand. He probably was implying for me to make a function to do whatever it needs to do, but I don't even understand what it's supposed to do. Confusing. Thanks for your help, if you know what he meant, I'd appreciate an explanation if you don't mind.

Posted: Wed Nov 23, 2005 4:17 pm
by hawleyjr
Create Function:

http://us3.php.net/manual/en/function.c ... nction.php


Can you please post what you have attempted to do? We can help walk you through it that way....

Posted: Wed Nov 23, 2005 4:19 pm
by hawleyjr
Also,

Here is another function which adds a SPAN class to selected words...

Code: Select all

function highlightText($string_to_highlt,$a_hightlight_words){
	
	
	if(!is_array($a_hightlight_words)){
		$a_hightlight_words = explode(" ", $a_hightlight_words);
	}
	
	$needle = join('|',$a_hightlight_words);
	

	// use the | to make preg_replace to use multiple values
	
	// use the /i to make case insensitive and \\0 not replace th word but simply to wrap the word.
	
	return preg_replace("/($needle)/i",'<span class="highlighttxt">\\0</span>',$string_to_highlt);
	

}

Posted: Wed Nov 23, 2005 4:46 pm
by Luke
Oh... he put in createfunction instead of create_function, but it still doesn't work and I'm sure its something I'm doing. I'll dink with it some more.

Posted: Mon Nov 28, 2005 6:43 pm
by Luke
I need help with this... :(

Posted: Tue Nov 29, 2005 1:11 am
by n00b Saibot
doesn't hawley's function work well :?: