General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 25th, 2003, 12:37 PM
mel mel is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Colorado
Posts: 7 mel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Highlight search terms

Some good search engines return results with the search term(s) highlighted or bolded in the results. ex: www.ask.com
Anyone know how this is done and if there is a way to do it in php?
I'm running apache/php/mysql - wanting to search the db & display using highlighting.

Thanks.

Reply With Quote
  #2  
Old April 25th, 2003, 01:14 PM
jpenn jpenn is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Washington, DC
Posts: 317 jpenn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 3 sec
Reputation Power: 16
PHP Code:
/****************************************
Highlighting Function
****************************************/
function highlight_words$text$keywords )
{
    foreach( 
$keywords as $word )
    {
        
$text preg_replace"/$word/i"'<span style="color:red;font-weight:bold;">' $word '</span>'$text ); 
    }
    return( 
$text );
}
/****************************************
Text String
****************************************/
$text 'The big red fox jumped over the fence.';
/****************************************
Array Of Keywords
****************************************/
$keywords[] = 'red';
$keywords[] = 'jumped';
/****************************************
Do It
****************************************/
echo( highlight_words$text$keywords ) ); 



Enjoy...
__________________
~ Joe Penn

We work for free to help make this a valuable resource on the internet. Do you appreciate the help - did we provide help that will help you prosper and help that has contributed to sharpening your current skill set?

Show your appreciation and purchase something from our Amazon Wishlist's - it's simple and a great way to say thank you.




Reply With Quote
  #3  
Old April 25th, 2003, 11:32 PM
Ben Rowe
Guest
Dev Articles Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
also if you want to search for all the words on the page, you can use some advanced header functions to retrieve the page content as a variable

$text = ob_get_contents();

ob_clean();

Then use jpenn's function to replace all the words on the page/.

However be carful, as if the words you are replacing are html, head, body, title, etc it will replace your html tags.

Reply With Quote
  #4  
Old April 26th, 2003, 10:16 AM
mel mel is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Colorado
Posts: 7 mel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
added explode

Thanks guys.

I had to add explode to get the foreach to work.

It turned out like this:

PHP Code:
 $keywords explode (" "$keywords);
                            
                            function 
highlight_words ($text$keywords)
                            {
                               foreach (
$keywords as $word)
                                {
                                    
$text preg_replace"/$word/i"'<span class="keyword">' $word '</span>'$text ); 
                                }
                                return (
$text);
                            }
                            echo (
highlight_words ($text$keywords)); 


Thanks again for your help.

-Mel

Reply With Quote
  #5  
Old March 1st, 2004, 04:56 PM
zonzon zonzon is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 1 zonzon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
highlight_words

it's a good way to highlight text, but if one of the searched/highlighted terms are one of (by example): style, background and they are already it the string because some replaced words before.

someone knows how to prevent this?

and is there a way to dont change the case of the letters? like And --> and after de highlight

thx in advance!

Reply With Quote
  #6  
Old March 2nd, 2004, 08:26 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to dhouston
Not changing certain key words is a little tricky but can be done by checking to see if they're nested within < and >. The other issue can be handled pretty easily:

PHP Code:
 $str="Is this some text?";
$find="/(is)/i";
$replace="<b>\${1}</b>";
$x=preg_replace($find,$replace,$str);
print 
"\n" $x "\n"


This outputs:

Code:
<b>Is</b> th<b>is</b> some text?


See that it maintains case. I'm also illustrating here that you have to be careful what you match on or you'll find partial matches within words. If you change your $find to "\b/(is)\b/i", you're looking for the given pattern within word boundaries, which in this case prevents the "is" in "this" from being found. The "i" at the end of the pattern makes the pattern case insensitive. The variable interpolated in the $replace string is a special variable that returns the first match found in the string.
__________________
Please don't PM me asking for solutions outside the scope of a thread.
Keeping all responses in a thread stands to help others who come along later,
which is after all what this forum's all about.

Reply With Quote
  #7  
Old March 2nd, 2004, 04:40 PM
tlflorek tlflorek is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 1 tlflorek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Is there a way to do the highlighting in vbscript or jscript?

Quote:
Originally Posted by jpenn
PHP Code:
/****************************************
Highlighting Function
****************************************/
function highlight_words$text$keywords )
{
    foreach( 
$keywords as $word )
    {
        
$text preg_replace"/$word/i"'<span style="color:red;font-weight:bold;">' $word '</span>'$text ); 
    }
    return( 
$text );
}
/****************************************
Text String
****************************************/
$text 'The big red fox jumped over the fence.';
/****************************************
Array Of Keywords
****************************************/
$keywords[] = 'red';
$keywords[] = 'jumped';
/****************************************
Do It
****************************************/
echo( highlight_words$text$keywords ) ); 



Enjoy...

Reply With Quote
  #8  
Old March 3rd, 2004, 03:25 AM
Spongy's Avatar
Spongy Spongy is offline
Alternately High
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Hilversum, Netherlands
Posts: 223 Spongy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 56 m 41 sec
Reputation Power: 15
Send a message via MSN to Spongy
There are other forums to ask this...
__________________
Work to live, don't live to work

Reply With Quote
  #9  
Old March 3rd, 2004, 08:02 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to dhouston
Sure, I'll bet you could do it in one of these other languages. Since we've explained to you exactly how to do it in PHP, though, I don't see why you woudn't go for it. I wouldn't use jscript, in any case, and don't know how powerful regular expressions are in vbscript, though I'd wager they're not nearly as flexible as what the preg functions in PHP provide.

Reply With Quote
  #10  
Old March 11th, 2004, 07:54 AM
DisasterMan DisasterMan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 7 DisasterMan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Exclamation Highlight case insensitive search results with multiple colours

I have been cracking at this for a while and am getting close to a comprehensive solution (bar highlighting the entire content of a page).
Take a look at this thread or if it doesn't work for any reason, find the thread titled
'RegExp to highlight search results'
started by 'stiantot' on 01-30-2004.

My technique works fine as long as you don't have any code (HTML tags etc.) in the result retrieved from the db that might match your search string.

Hope this helps,
DisasterMan

Last edited by DisasterMan : March 11th, 2004 at 08:02 AM. Reason: ickle typos...

Reply With Quote
  #11  
Old April 21st, 2004, 12:25 PM
rabidgopher04 rabidgopher04 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 5 rabidgopher04 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
highlighting keywords individually

All your help has been great so far as I have read both threads mentioned here, but I am wondering if someone could help me make the final step.

My code only highlights the keywords if they all appear right next to each other and not each keyword individually.

here is my code so far:
PHP Code:
<?
 
 
// Highlight search words in document passed on from local search
 
if (isset ($HTTP_GET_VARS['highlight']))
 {
     
$searchwords explode("+"str_replace(array("-","*","~","\"","(",")","<",">","\\"),"",$HTTP_GET_VARS['highlight']));
     
     function 
highlight_words($page_content)
     {
         global 
$searchwords;
         
$colors = array('80A000','922292','990000','707070','008080'  ,'999900');
         
$i=0;
     
         foreach (
$searchwords as $word)
         {
             if (
$i>count($colors)-1)
             {
                 
$i=0;
             }
 
             
$page_content eregi_replace($word'<span style="background: #'.$colors[$i].';">'."\\0".'</span>'$page_content);
             
$i++;
         }
         return 
$page_content;
     }
 
     echo (
highlight_words($page_content));
 }
 else
 {
     echo 
$page_content;
 }
 
?>


Some helpful notes: keywords get passed on as new+york+yankees and so I split them by the "+"

Thanks to anyone that can help me understand this! Please let me know if you need anymore information.

Reply With Quote
  #12  
Old April 21st, 2004, 01:52 PM
DisasterMan DisasterMan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 7 DisasterMan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Why is that then?

Hmm, you've got me stumped there.
I can't see why it would only highlight the words consecutively.
Your code works in very similar way to mine, so I can only suggest eliminating the differences.

There are a couple of points, but I would be surprised if they affected this particular issue. But who knows? Give it a whirl and see what happens:

1) There is no need for your function 'highlight_words' to be inside your 'if' statement, I don't believe it will save on processing, as the function will be ignored until/unless called. This also makes the function available for repeated use. You could then stick it in a functions.php file and put
<? include("functions.php");?>
at the top of the php file. You can then do the same thing over many different files without having the function hard coded into every page.
If you are going to call the function repeatedly then $searchwords needs to be set outside of the 'if' statement too

2) You appear to be processing the entire output of the page. I guess you are pulling the entire page code from a database, but even so, you run the risk of breaking the code with highlights in the case of a search matching some of that code. There are a lot of potential conflicts (how can you guarantee that no-one will ever search for 'target' 'link' 'form' 'method' or 'action' to give but a few examples) so I would advise running your highlight function only on the text rather than the total page. This should also reduce processing overheads as there will be no need to search lots of extraneous code or other content, as well as avoiding unwanted highlighting.
For example, if you have a menu item 'teams' you don't want that to be highlighted when someone searches for 'teams'.

In order to do this you would obviously have to include the function-call in the $page_content data pulled from database, where highlightable text would be surrounded by the output buffer commands:
PHP Code:
<? ob_start("highlight_words");?> Searchable Page Content <? ob_end_flush(); ?>


If, as it seems from your code, you want to make highlighting optional you could always do it like this:
PHP Code:
<?
if ($_GET['highlight'])
{
    
ob_start("highlight_words");
}
?>
    Searchable Page Content
<?
if ($_GET['highlight'])
{
    
ob_end_flush();
}
?>

And repeat that around however many viable highlight areas you have.
Note the use of shorthand here:
if ($foo)
is the same as
if (isset($foo))
and
$HTTP_GET_VARS['foo']
is the same as
$_GET['foo']
...nice and handy...

In order to do the highlight as described above using output buffer, all instances of $page_content WITHIN the function must be replaced with $buffer

3) I don't think you even need an if statement - if $HTTP_GET_VARS['highlight'] is not set, there will simply be no terms to highlight.

I have given you a few variable options, but this is how I reckon it should work best and simplest:
PHP Code:
<?
function highlight_words($page_content)
{
    global 
$searchwords;
    
$colors = array('80A000','922292','990000','707070','008080'  ,'999900');
    
$i=0;

    foreach (
$searchwords as $word)
    {
        if (
$i>count($colors)-1)
        {
            
$i=0;
        }
        
$page_content eregi_replace($word'<span style="background: #'.$colors[$i].';">'."\\0".'</span>'$page_content);
    
$i++;
    }
    return 
$page_content;
}

$searchwords explode("+"str_replace(array("-","*","~","\"","(",")","<",">","\\"),"",$_GET['highlight']));

// Highlight search words in document passed on from local search
ob_start("highlight_words");
echo 
$page_content;
ob_end_flush();?>


I hope this gets you somewhere (or at least on the road to somewhere), although I still don't understand your consecutive words problem.

Reply With Quote
  #13  
Old April 21st, 2004, 04:28 PM
rabidgopher04 rabidgopher04 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 5 rabidgopher04 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
DisasterMan,
Thanks for the tips - I'm still learning about the more complicated things of PHP so I do it all the long way.

$page_content is actually just a field from the database that contains the text from the article. The only markup would be <p>, <blockquote> and lists so I am not worried about the markup being altered by the highlight script. And the only time I am using the highlight thing is for $page_content so I am not worried about having to reuse it.

Yes, you were right, I was trying to make it optional, but your way works just fine as long as I put an @ in front of the eregi_replace() in case there is nothing to highlight to suppress the warning it gives.

So I replaced what I had with what you supplied and it still does the same thing. Do you think there are any other factors involved? Before I append the highlight terms to the URL I run them through this little bit of code:
PHP Code:
 $query preg_replace('/[^a-z]/i','+',$query); 


Then I just append it to the URL of each of the search results.

There is no other manipulation of $page_content other than the highlight section I am trying to add.

Reply With Quote
  #14  
Old April 22nd, 2004, 01:55 AM
rabidgopher04 rabidgopher04 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 5 rabidgopher04 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I figured out what the deal was. I was thinking that the "+" was part of the string for $highlight so I had to explode it by "+" not " " (if that makes no sense please let me know).

Needless to say i got it to work and it is charming. Thanks for eveything!

Reply With Quote
  #15  
Old April 22nd, 2004, 09:51 AM
DisasterMan DisasterMan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 7 DisasterMan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Nice one!

Well that's what I get for trusting everything you say!
Glad it's all sorted!

How about letting us see it in action?

I haven't put my URL up yet as the archive has a lot of personal info in it that verious people are moderating, but I will do when we go public!

Reply With Quote
  #16  
Old April 22nd, 2004, 11:34 AM
rabidgopher04 rabidgopher04 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 5 rabidgopher04 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
How about letting us see it in action?

All right, sounds good. http://www.aboutcatholics.com/viewp...ay+or+the+bible

I think I will end up changing the colors because they are a little dark for some monitors. I also added another feature and that was to not highlight words that are 3 characters or less. I think that will prevent most words like "the", "and", "or", "but", etc. from being being highlighted. Unfortunately that leaves out words like "sad", "cat", "hat", "do", etc. but maybe someone could help me incorporate a common words filter. My question is will that slow down the script significantly to check against common words? Perhaps I could filter out the common words in search.php but it might slow the search down.

One thing I would like to figure out is exact word matching. I read the posts about how it was difficult to do this and I'm not good with RegEx so I don't know if I 'm even going to try to figure that out.

Any ideas?

Reply With Quote
  #17  
Old April 22nd, 2004, 01:23 PM
DisasterMan DisasterMan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 7 DisasterMan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Absolutely, pastel colours would be much better with your colour scheme. Particularly the red (#990000) is far to dark to read black text clearly. I use the script on a black background (tempted to say I'm working on aboutsatan.com - should there be some rules about misrepresentation? - new+york+yankees is slightly different from lent+ash+wednesday+or+the+bible!) with white text where the colours, although vile, are clear.

Very professional looking site, and the writing and research apears to be of high quality, wether one is a believer or not - keep up the good(!) work...

Reply With Quote
  #18  
Old April 22nd, 2004, 03:18 PM
rabidgopher04 rabidgopher04 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 5 rabidgopher04 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by DisasterMan
should there be some rules about misrepresentation? - new+york+yankees is slightly different from lent+ash+wednesday+or+the+bible!

Very professional looking site, and the writing and research apears to be of high quality, wether one is a believer or not - keep up the good(!) work...

Ha ha, well I didn't want anyone to deny me help because they didn't like my site

Thanks for the compliments and thanks for the help.

Reply With Quote
  #19  
Old May 17th, 2004, 01:30 PM
bjand bjand is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 1 bjand User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
hi! i added some code, coz i needed highlight search result
// here I search from database some words, but without control errors
// here must be strip and addslashes stuff too and control,
// that searching text must be at least "something"
// and hightlight function moded too, cos if i searched "red ed" it gave me some html code
// maybe i will help somebody somehow
function db_search_all($searchfor='')
{
global $db;
$keywords = explode(' ',$searchfor);

foreach( $keywords as $word )
{
if ($i == 0) $con = " "; else $con = " OR ";
$where .= " $con (desc_lang1 LIKE '%%$word%%') ";
$i++;
}
$query = "SELECT id,textfield FROM datatable WHERE $where ";
$stmt = mysql_query($query);

while ($row = mysql_fetch_array($stmt))
{
echo(highlight_words($row[1], $keywords));
echo "<hr>";
}
}


function highlight_words( $text='', $keywords='')
{
$ntext = '';
foreach( $keywords as $word )
{
$ntext .= preg_replace( "/$word/i", '<span style="color:red;font-weight:bold;">' . $word . '</span>', $text );
}
return($ntext);
}

Reply With Quote
  #20  
Old June 1st, 2004, 04:03 AM
dmchau dmchau is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 3 dmchau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Asp?

Is it possible to do this in ASP, if yes then how?

Reply With Quote
  #21  
Old July 9th, 2004, 01:19 AM
soj soj is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 2 soj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
This should do the job -

<%
Function Highlight_SearchTerms(Text, SearchTerms)
Temp = Text
TheTerms=Split(SearchTerms," ")
x=0
Do Until x=>UBound(TheTerms)
Temp=Replace(Temp,TheTerms(x),"<span style='color:red;font-weight:bold;'>" & TheTerms(x) & "</span>"
x=x+1
Loop
Highlight_SearchTerms = Temp
End Function
%>

Just use it like so:
<%
blah = Highlight_SearchTerms("my name is david", "name my")
response.write blah
%>

Reply With Quote
  #22  
Old October 20th, 2004, 10:10 AM
DisasterMan DisasterMan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 7 DisasterMan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Lightbulb

Quote:
Originally Posted by rabidgopher04
Ha ha, well I didn't want anyone to deny me help because they didn't like my site


But maybe I HATE baseball?


So here is an update that has been a long time coming - I had let the matter lie as I had it working to my satisfaction on this searchable archive I was working on. Then the other day my SysAdmin pulls this little corker out of the hat:
PHP Code:
 $regex '#(?!<.*?)(%s)(?![^<>]*?>)#si' 

Ahhhhh! The holy grail has arrived - the tag proof regex - I have seen it in action but I have not yet invoked its powers on my own incantations!

Any one has problems, get in touch, I will let you guys know how the results turn out...

UPDATE! Is tested and working, but with preg_replace instead of eregi_replace.
See the whole function in this thread titled
'RegExp to highlight search results'


MuffJam - London's greatest Hip Hop/Reggae nights

Reply With Quote
  #23  
Old February 10th, 2005, 04:30 PM
potentila potentila is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 1 potentila User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 57 sec
Reputation Power: 0
This function might be what you are looking for:

http://www.really-useful-stuff.integri.net/mwiki/index.php/Highlighting_search_strings_in_PHP

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Highlight search terms


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap