1

I'm using this function from here, which is:

// highlight search keywords 
function highlight($title, $search) {
preg_match_all('~\w+~', $search, $m);
if(!$m)
    return $title;
    $re = '~\\b(' . implode('|', $m[0]) . ')\\b~i';

return preg_replace($re, '<span style="background-color: #ffffcc;">$0</span>', $title);
}

Which works great, but only for titles. I want to be able to pass an array that contains $title and $description.

I was trying something like this:

$replacements = array($title, $description);

// highlight search keywords 
function highlight($replacements, $search) {
preg_match_all('~\w+~', $search, $m);
if(!$m)
    return $replacements;
    $re = '~\\b(' . implode('|', $m[0]) . ')\\b~i';

return preg_replace($re, '<span style="background-color: #ffffcc;">$0</span>', $replacements);
}

It isn't working. It's passing an array as the title, and not highlighting the description (although it is actually returning a description). Any idea how to get this working?

1
  • Your indentation looks messed up. Please fix it. Commented Apr 30, 2011 at 9:25

2 Answers 2

2

I would personally leave the original function as only operating on one parameter rather than an array. It would make your calling code nice and clear;

$titleHighlighted = highlight($title, $searchKeywords);
$descriptionHighlighted = highlight($title, $searchKeywords);

However, I would rewrite your function to use str_ireplace rather than preg_replace;

function highlight($contentBlock, array $keywords) {
        $highlightedContentBlock = $contentBlock;

        foreach ($keywords as $singleKeyword) {
                $highlightedKeyword = '<span class = "keyword">' . $singleKeyword . '</span>';
                $highlightedContentBlock = str_ireplace($singleKeyword, $highlightedKeyword, $highlightedContentBlock);
        }   

        return $highlightedContentBlock;
}

This rewritten function should be more simple to read and does not have the overhead of compiling the regular expressions. You can call it as many times as you like for any content block (title, description, etc);

$title = "The quick brown fox jumper over ... ";

$searchKeywords = array("quick", "fox");
$titleHighlighted = highlight($title, $searchKeywords);

echo $titleHighlighted; // The <span class = "keyword">quick</span> brown ...
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for the help and the heads up that str_ireplace() is quicker than a regex.
This will not work for search queries like "a a", because the keywords are "a" and "a" and for the first keyword the html will look like "<span class='xyz'>a</span>", and for next keyword "a" it will destroy the HTML and will provide "<sp<span class='xyz'>a</span>n> ..." it will actually replace the "a" contained in "<span>" string also..
0

have you try to change ?

$m[0]

with

$m[0][0]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.