Jump to content


Simple HIGHLIGHT Function


8 replies to this topic

#1 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 17 August 2007 - 06:38 PM

Okay, Im trying to get this function working for my search for the forums.
Now I want it to highlight the word on the page...

My Code:
	$hlight = htmlspecialchars($_GET['highlight']);

function hlight($word){	   
	if($hlight){
		$word = eregi_replace($hlight, '<span style="color:#FFFF99;">' . $hlight . '</span>', $word);
	} else {
		$word = $word;
	}
	return $word;
}

Now i use it like so and all the variables are defined.
www.site.com/index.php?showtopic=1&highlight=testing

Now either way If i use it with or without &highlight=testing It still displays the regular text/content or $word without the highlighting.

Can anybody help. It's probably something stupid lol :P :)

#2 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 17 August 2007 - 08:51 PM

Ok, two things.

1. You are trying to access a variable outside of the function's scope without globalizing the variable.
2. You aren't invoking the function.

This is more of how it should look. No use in using a eregi_* function when you aren't using regular expressions; preg_* functions are much faster anyways.

$content = 'Your content string.';
if(isset($_GET['highlight'])){
  $hlight = htmlspecialchars($_GET['highlight']);
  $content = str_replace($highlight, '<span style="color:#ff9;">'.$highlight.'</span>', $content);
}


#3 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 17 August 2007 - 09:05 PM

Hmm weird, I did that first, and it did nothing. hmm... That works now. I dont see what I did wrong.'

  $topicx = $topic['text'];
if(isset($_GET['highlight'])){
  $highlight = htmlspecialchars($_GET['highlight']);
  $topicx = str_replace($highlight, '<span style="color:#ff9;">'.$highlight.'</span>', $topicx);
}

Dosnt work >_>

Edited by Braunson, 17 August 2007 - 09:24 PM.


#4 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 18 August 2007 - 07:49 AM

Explain 'doesnt work'. Is it simply not highlighting the text, or what?

I've tested the code with some dummy text and it works fine...

#5 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 19 August 2007 - 05:41 PM

Yes, it just returns the text. Sorry lol. It dosn't highlight or anything. Here's how im using it...

		$topics = mysql_query("SELECT * FROM `forum_threads` WHERE `id` = '".$id."'");
		if($topics)
		{
			$topic = mysql_fetch_array($topics);
			echo ("<br />
							<div style=\"background: #F8F8F8; padding: 5px;\" align=\"left\">");

 $topicx = $topic['text'];
if(isset($_GET['highlight'])){
  $highlight = htmlspecialchars($_GET['highlight']);
  $topicx = str_replace($highlight, '<span style="color:#ff9;">'.$highlight.'</span>', $topicx);
}
			echo("				</div>
			");
		   }

Thanks Demonslay!

#6 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 19 August 2007 - 07:59 PM

You have to echo your text...

$topics = mysql_query("SELECT * FROM `forum_threads` WHERE `id` = {$id}") or die(mysql_error());
if(mysql_num_rows($topics) > 0){
  $topic = mysql_fetch_array($topics);
  echo '<br /><div style="background:#f8f8f8;padding:5px;" align="left">';
  $topicx = $topic['text'];
  if(isset($_GET['highlight'])){
	$highlight = htmlspecialchars($_GET['highlight']);
	$topicx = str_replace($highlight, '<span style="color:#ff9;">'.$highlight.'</span>', $topicx);
  }
  echo $topicx.'</div>';
}


#7 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 19 August 2007 - 09:13 PM

Hmm What am I doing wrong =\
Still dosn't work for me.

#8 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 20 August 2007 - 02:19 PM

Check the source of the page and check that the text is indeed being wrapped by the span tags.
And for an actual 'highlight' you'd probably be better in setting a background color rather than a text color in the span's style.

#9 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 06 September 2007 - 04:37 PM

I have checked the source, it dosn't add the span at all. and Im going to add the background after I get it working. Thanks tho :o

Could something be possible conflicting with this, since what displays the topics from a form is inside a topic...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users