lwc Posted November 16, 2006 Share Posted November 16, 2006 I want to have Google like search results. That means:1) Highlighting the searched keywords, but still only presenting the phrases around them and not the entire page.2) Presenting only the sentences in which the searched keywords are in. And keep in mind those sentences could be:2)A. At the start of the page.2)B. At the middle of the page.2)C. At the end of the page.Currently, I managed to do just the highlighting, but even that came with a price - it forces upper/lower capitalization based on the search. For example, if you looked for the word "php", every word that is spelled "PHP" would be changed to "php" in order to be highlighted.Or in PHP language:[code]$results = str_ireplace($searchword, '<font color=red>' . $searchword . '</font>', $results);[/code]I had to use str_[b]i[/b]replace because otherwise it's a case sensitive search, which I don't want!So again, what I want is just like Google would have it: for example, searching for the word "php" would also highlight the word "PHP", but without turning it into "php".Thanks! Quote Link to comment Share on other sites More sharing options...
Nicklas Posted November 16, 2006 Share Posted November 16, 2006 use regular expressions like [url=http://www.php.net/preg_replace]preg_replace()[/url]ex[code=php:0]<?php$string = "Highlight everything with php, like, PhP and www.phpfreaks.com";$searchword = "php";echo preg_replace("/($searchword)/is", '<span style="color: #ff0000">\\1</span>', $string);?>[/code] Quote Link to comment Share on other sites More sharing options...
lwc Posted November 16, 2006 Author Share Posted November 16, 2006 Please delete this post. Quote Link to comment Share on other sites More sharing options...
lwc Posted November 20, 2006 Author Share Posted November 20, 2006 Thanks, worked like a charm and doesn't even require PHP v5 like str_ireplace!I've added another issue to this topic - can you help out with it too? Quote Link to comment Share on other sites More sharing options...
lwc Posted January 11, 2007 Author Share Posted January 11, 2007 Come on, no one knows this? :( Quote Link to comment Share on other sites More sharing options...
effigy Posted January 12, 2007 Share Posted January 12, 2007 I suppose you could use PREG_OFFSET_CAPTURE, grab the content before (offset - amount) and after ((offset + match_length) + amount) the match, then regex/process the before and afters as needed. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.