Syntax Highlighting and Preserving Case

Hi im implementing a seatch using mysql qhich takes in a number of keywords.

Im trying to perform Syntax Highligting and have so far used the following.

$search_details['name_short'] = str_ireplace($term,"<b>$term</b>",$search_details['name_short']);

This works great however it uses the case of whatever $term is rather than the case of whatever it was before so

PHPDevelopment

and a search for php returns <b>php</b>development.

Im guessing this is going to have to be done with a preg_replace but I cant quite remeber how to return the original string back as the replacement. If it can search for an array as $term that would also be a bonus im 99% certain theres a way of doing this too.      
LVL 13
Robin HickmottSoftware DeveloperAsked:
Who is Participating?
 
hernst42Connect With a Mentor Commented:
I use the following code for highlighting which does not highlight the searchtext in html-tags
if (!empty($_REQUEST['searchtext'])) {
    while (($pos = strpos($text, '<')) || substr($text,0,1) == '<') {
        // Extract the text before the beginning of a html-tag
        $plaintext = substr($text, 0, $pos);
        echo preg_replace('/('.preg_quote($_REQUEST['searchtext'], '/').')/i', '<span class="hlsearch">\1</span>', $plaintext);
        // No get the tag with it's content and print it unchanged.
        $text = substr($text, $pos);
        $end = strpos($text, '>');
        if ($end == 0) {
            //no ending of an opend tag found, print all
            echo $text;
            $text = '';
        } else {
            echo substr($text,0,$end+1);
            $text = substr($text, $end+1);
        }
    }
    echo preg_replace('/('.preg_quote($_REQUEST['searchtext'], '/').')/i', '<span class="hlsearch">\1</span>', $text);
} else {
    print $text;
}

Open in new window

0
 
Robin HickmottSoftware DeveloperAuthor Commented:
Well the information is coming straight from a mysql database rather than syntax highlighting on a page so doesent need to be quite so overkill buy ill shall take a look at the basics and let you know :)
0
 
Robin HickmottSoftware DeveloperAuthor Commented:
The following seems to work but doesent stop at the end of the search term.
$search_details['name_short'] = preg_replace('/('.preg_quote($terms, '/').')/i', '<b>\1<b>', $search_details['name_short']);

Open in new window

0
 
Robin HickmottSoftware DeveloperAuthor Commented:
Scrap that all working ta :)
0
 
Robin HickmottSoftware DeveloperAuthor Commented:
For reference it was a case of the missing / :)

$search_details['name_short'] = preg_replace('/('.preg_quote($terms, '/').')/i', '<b>\1</b>', $search_details['name_short'])
0
All Courses

From novice to tech pro — start learning today.