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 8353447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:15:43+00:00 2026-06-09T09:15:43+00:00

I want to highlight my keyword search text by using preg_replace in php but

  • 0

I want to highlight my keyword search text by using preg_replace in php but the problem is it won’t work with parentheses string ( and )

My function look like this…

$text_oritinal = "25 Hours: Colour In White (CD)";

function highlight($text_search, $text_original) {
  $str = preg_replace('#'. $text_search .'#i', '<span style="background-color:#FFFF66; color:#FF0000;">\\0</span>', $text_original);
  return $str;
}

Full original text is "25 Hours: Colour In White (CD)"

For example. If I use the keyword

$text_search = "25 Hours: Colour In White";

It return good replaced keyword with highlight background and text color.

<span style="background-color:#FFFF66; color:#FF0000;">25 Hours: Colour In White</span> (CD)

but! if I use this keyword included parentheses string ( and )

$text_search = "25 Hours: Colour In White (CD)";

It NOT return replaced background and text color.

I think it stuck with parentheses string ( and )

The question is how to highlight all text keyword matched without any problem with parentheses string ( and )?

Please share your idea. Thanks 🙂

  • 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-06-09T09:15:45+00:00Added an answer on June 9, 2026 at 9:15 am

    You should call preg_quote:

    function highlight($text_highlight, $text_search) {
      $str = preg_replace('#'. preg_quote($text_highlight) .'#i', '<span style="background-color:#FFFF66; color:#FF0000;">\\0</span>', $text_search);
      return $str;
    }
    

    Explanation:

    Parenthesis are used to create submatches. Using them in your search string, will cause undesired results as they are reserved characters. You can prevent this, and other characters by using preg_quote which escapes regex reserved characters.

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

Sidebar

Related Questions

I have a PHP search script and I want to highlight the keywords that
I want to highlight a <span> using jQuery.effect('highlight'), but it fades out after a
I want to highlight the current menu you have click. I'm using CSS, but
I'm using this function from here , which is: // highlight search keywords function
I have a longer text and some keywords. I want to highlight these keywords
I am using jquery datepicker for my application. I want to highlight the dates
I want a regular expression to highlight text (underline) in sms like default sms
I have two tables and they highlight as expected, but I only want the
I want to highlight text in a specific position. Something like this: highlight with
I have an items template and I want to highlight some text in a

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.