Welcome Guest, Not a member yet? Register   Sign In
highlight searched string
#1

[eluser]nirbhab[/eluser]
hi,
i am making a search engine module for my site, please suggest me a way to highlight the string or a sentence in searched output,
eg. google after displaying the result bolds the string.

Example:
CodeIgniter - Open source PHP web application framework
CodeIgniter: an open source Web Application Framework that helps you write PHP programs.
#2

[eluser]Michael Wales[/eluser]
Just do a str_replace to apply a class to the string, then setup your CSS.


[code]
<?php
$str = str_replace($query, '<span class="bold">' . $query . '</span>', $str);
?&gt;
#3

[eluser]xwero[/eluser]
My best guess is you would have to do it with javascript. The page where you display your results has the search string and based on that it searches for the words of that string.
Code:
$(function(){
   var searchstring = $('#searchstring').text();
   var searchwords = searchstring.split(' ');
   for(var i = 0;i < searchwords.length;i++)
   {
      if(searchwords[i].length > 1) // exclude one character words or punctuation marks
      {
         $('p.result').each(function() { $.highlight(this, searchwords[i].toUpperCase()); }));
      }
   }
});
The highlight method is a jquery plugin.
#4

[eluser]nirbhab[/eluser]
Thank you guys, let me try implementing your ideas and code, concept seems right!
#5

[eluser]cinewbie81[/eluser]
[quote author="walesmd" date="1193224976"]Just do a str_replace to apply a class to the string, then setup your CSS.


[code]
&lt;?php
$str = str_replace($query, '<span class="bold">' . $query . '</span>', $str);
?&gt;[/quote]

I'm looking for the same thing .. thanks again ..
but if I'm not mistaken, str_replace function is only works for php5 right ?? That's bcos i found it on other site, there's different version of code for different version of php .. please see the link below :

http://www.whatstyle.net/articles/6/disp...s_with_php

I'm using php5, please explain if str_replace function will works in php4 or not .. thx
#6

[eluser]xwero[/eluser]
I'm using str_replace in php4 there is no problem with it.
#7

[eluser]cinewbie81[/eluser]
cheers ..
btw, is there anyway for me to IGNORE the CASE SENSITIVITY using the str_replace() function mentioned above ???
#8

[eluser]xwero[/eluser]
using preg_replace Smile
#9

[eluser]John_Betong[/eluser]
Why not use CodeIgniter's Text helper?

Code:
highlight_phrase()

Will highlight a phrase within a text string. The first parameter will contain the
original string, the second will contain the phrase you wish to highlight. The third and
fourth parameters will contain the opening/closing HTML tags you would like the phrase
wrapped in. Example:

$str = "Here is a nice text string about nothing in particular.";

$string = highlight_phrase($string, "nice text", '<span style="color:#990000">', '</span>');
The above text returns:

Here is a nice text string about nothing in particular.
#10

[eluser]xwero[/eluser]
[quote author="John_Betong" date="1193248162"]Why not use CodeIgniter's Text helper? [/quote]
It uses preg_replace Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB