Jump to content

Google-Like highlighting


pantinosm

Recommended Posts

I use this script here which someone gave to me.

<?php 
function highlight($str) {
$tmp = '';


foreach(explode(' ', $str) as $word)
$tmp .= "<span style=\"background-color:pink\">$word</span> ";

return trim($tmp);

}
?>

 

This function runs when i call echo preg_replace("/$searchterm/ise", 'highlight("\\0")',  $result);

 

The problem is that not all keywords are highlighted. It leaves many words back. Sometimes ithighlights, sometimes no. Why is this happenning?

Link to comment
Share on other sites

pantinosm,

 

I couldn't get your code to work... Using your preg_replace function had a parsing error ( Delimiter must not be alphanumeric or backslash ).

 

Also, I could never get it to call your highlight function... Don't have a clue why not.  :-\

 

So, I played around with your concept a bit, and came up with the following generic highlight function.

 

Scot L. Diddle, Richmond VA

 

highlightString.php

 


<?php

require_once('include/wordHighlight.php');

	$string = 'Now is the time for all good programmers... etc';	
highlight("$string", "programmers");

echo $highlightedString;

?>

 

wordHighlight.php

 


<?php 

function highlight($str, $slectedWord)  {

	global $highlightedString; 

	$highlightedString = '';

	foreach(explode(' ', $str) as $word) {

		$currentWordLength = strlen($word);

		$lastChar = substr($word,$currentWordLength-1) . ' ';			

		if (substr($word,$currentWordLength-2,2) == '..' ) {
			$lastChar = '..';
		}

		if (substr($word,$currentWordLength-3,3) == '...' ) {
			$lastChar = '...';
		}			


		$word =  str_replace(',', ' ', $word); // replace , by space
		$word =  str_replace('.', ' ', $word); // replace . by space
		$word =  str_replace('?', ' ', $word); // replace ? by space
		$word =  str_replace(':', ' ', $word); // replace ? by space
		$word =  str_replace('!', ' ', $word); // replace ? by space
		$word = trim($word);

	    if  ($word == $slectedWord) {
		$highlightedString .= ' ' . "<span style=\"background-color:pink\">$word</span>$lastChar";
	    }
	    
	  else {
		  
		$highlightedString .= ' ' . $word;

	  }

	}

	return trim($highlightedString);

}


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.