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


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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 :)
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

Robin HickmottSoftware DeveloperAuthor Commented:
Scrap that all working ta :)
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'])
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.