Replacing keywords with highlighted keywords.

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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Replacing keywords with highlighted keywords.

Post 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.
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

Code: Select all

str_replace($keyword, "<span style=\"background-color: #FFFF00\">$keyword</span>", $result['column'])
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Re: Replacing keywords with highlighted keywords.

Post 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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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?
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Check out the link I posted. Feyd provides a working function there...
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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....
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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);
	

}
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I need help with this... :(
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

doesn't hawley's function work well :?:
Post Reply