Text highlighting in search results Jul22 '05

Feedback

# (1 of 10): Jennifer Grucza » jennifergrucza.com

3 hours, 26 minutes after the fact. (Fri 22 Jul 2005, 11:51 AM CST)

Does PHP do regular expressions? In Java, I would do it this way:

searchResults.replaceAll("(?i)php str_replace", "php str_replace");

The "(?i)" tells it that it's case-insensitive.

# (2 of 10): Matthom

3 hours, 42 minutes after the fact. (Fri 22 Jul 2005, 12:07 PM CST)

Jennifer, PHP does indeed do regular expressions - and that is certainly a great solution for the case sensitivity issue.

Previous comment Return to entry

# (3 of 10): Joshua Street » joahua.com/blog

13 hours, 8 minutes after the fact. (Fri 22 Jul 2005, 9:33 PM CST)

You could explode (the PHP function) the search string first so you can catch instances separately. Would also help when actually building your raw search query (i.e. in SQL) for the database... explode then add "AND" between terms, or whatever. I don't know if MySQL already does this, though...?

Previous comment Return to entry

# (4 of 10): Joshua Powell » jepowell.com

1 year after the fact. (Sun 23 Jul 2006, 4:00 PM CST)

I know this article is getting on a bit, but I found it at the top of a Google search so it’s still relevant :-)

As of PHP5, there is a case insensitive equivalent of str_replace, stri_replace: http://au.php.net/manual/en/function.str-ireplace.php

Previous comment Return to entry

# (5 of 10): Jim Mcwhitter » bayofislands.net

2 years after the fact. (Thu 09 Aug 2007, 1:56 AM CST)

I guess though you wouldn't want to just search and replace everything that matches, otherwise you would replace tagged contents (eg search for 'spacer' and suddenly 'spacer.gif' becomes 'spacer.gif)?

Previous comment Return to entry

# (6 of 10): Hellclanner » betapod.net

2 years, 2 months after the fact. (Fri 12 Oct 2007, 8:17 PM CST)

this kind of parsing of the words is seriously bad. though a good suggestion. what if you want to highlight the word "span" and it is found as HTML tags, it'll cause the HTML to deform.

Previous comment Return to entry

# (7 of 10): Sebastian Schepis » zoomitin.com

2 years, 4 months after the fact. (Wed 12 Dec 2007, 1:26 PM CST)

Here's a way to do this in PHP that will bold text in a string. The nice thing about this method is it case insensitive, and will preserve the original case when bolding - so if you have a string 'Mary had a little lamb' and your keyword to bold is 'mary', then the resulting string will be 'Mary had a little lamb' instead of 'mary had a little lamb' if using the other solutions on this page. Oh, and it also will bold every word present in the string from your original keywords - you don't need to have an exact phrase in your string.

Here's the method:

function bk($strtobold, $keywords) { $patterns = Array(); $replaces = Array();

if ($keywords != "") {  
    $words=explode(" ", $keywords);
    foreach($words as $word){
        $patterns[]='/'.$word.'/i';
        $replaces[]='<b>$0</b>';
    }
    return preg_replace($patterns, $replaces, $strtobold); 
} else return $strtobold;

}

Previous comment Return to entry

# (8 of 10): Yari Mcgauley » evolvedwebsites.com.au

2 years, 8 months after the fact. (Mon 14 Apr 2008, 8:56 PM CST)

Thanks Sebastian, your function worked perfectly!

Previous comment Return to entry

# (9 of 10): Ash

2 years, 9 months after the fact. (Fri 25 Apr 2008, 12:12 PM CST)

Sebastian,

Your solution does not work.

for example:

$keywords = "test1 test2 b span"; $words = "This is a test1 of the test2 bold span";

Previous comment Return to entry

# (10 of 10): Adriaan Nel » adriaannel.com

3 years after the fact. (Tue 22 Jul 2008, 3:30 PM CST)

I'm sorry but Sebastian's function doesn't work, it will replace text within a tags' href attribute for example, which will cause the link to be broken.

For this to work properly, you will first need to ensure you aren't replacing text within any html tag attribute.

Previous comment Return to entry

RSS feed for comments on this post

Leave feedback

Feedback

Input format: The editor controls below will assist with Markdown syntax.

Status

Sub-status

Your info

Return to entry.

I’ve always been intrigued with search results that highlight, in the document, the search terms that were requested. In this article, I briefly explain some ways to achieve this. I’d like reader feedback to fill in some of the holes.

You are at the feedback permalink page for: Text highlighting in search results

Read more...