Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 868707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:11:06+00:00 2026-05-15T10:11:06+00:00

I’m trying to make search terms bold in this search script that I’m making.

  • 0

I’m trying to make search terms bold in this search script that I’m making. The trouble is that I can’t get it to work case insensitively.

function highlight($term,$target){
    $terms = explode(" ", $term);
    
    foreach($terms as $term){
        $result = (eregi_replace($term, "<strong>$term</strong>", $target));
    }
    return $result;
}

That is the function I have so far. It says on PHP.net that eregi_replace() is case-insensitive, but it’s obviously not working for some reason.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:11:07+00:00Added an answer on May 15, 2026 at 10:11 am

    The ereg_* (POSIX regular expression) functions are deprecated as of PHP 5.3 and have not been suggested for a long time. It is better to use the PCRE (preg_*) functions (such as preg_replace).

    You can do so by creating a case-insensitive regular expression, and then wrapping matches in <strong> tags:

    function highlight($term, $target)
    {
      $terms = array_unique(explode(" ", $term)); // we only want to replace each term once
      foreach ($terms as $term)
      {
        $target = preg_replace('/\b(' . preg_quote($term) . ')\b/i', "<strong>$1</strong>", $target);
      }
    
      return $target;
    }
    

    What this does is first call preg_quote on your $term so that if there are any characters that have meaning in a regular expression in the term, they are escaped, then creates a regular expression that looks for that term surrounded by word boundaries (\b — so that if the term is “good” it won’t match “goodbye”). The term is wrapped in parentheses to make the regular expression engine capture the term in its existing form as a “backreference” (a way for the regular expression engine to hang on to parts of a match). The expression is made case-insensitive by specifying the i option. Finally, it replaces any matches with that same backreference surrounded by the <strong> tag.

    $string = "The quick brown fox jumped over the lazy dog. The quicker brown fox didn't jump over the lazy dog.";
    $terms = "quick fox";
    highlight($terms, $string);
    // results in: The <strong>quick</strong> brown <strong>fox</strong> jumped over the lazy dog. The quicker brown <strong>fox</strong> didn't jump over the lazy dog.
    

    If you’d like a good tutorial on regular expressions, check out the tutorial on regular-expressions.info.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.