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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:35:26+00:00 2026-06-08T10:35:26+00:00

I am doing a search and replace in php on a string. If my

  • 0

I am doing a search and replace in php on a string.

If my example string is

My name is Matthew Scott Hailwood

Then when the function is run with the search of o the output becomes (split over multiple lines for readability)

My name is 
Matthew 
Sc<span class="highlight">o</span>tt 
Hailw<span class="highlight">o</span><span class="highlight">o</span>d

That part works perfectly.

My css class then has

.highlight{
    font-weight: bold;
    background-color: yellow;
    border: 1px dotted #a9a9a9;
}

Which also works perfectly.

But in the case of double letters e.g. the oo in the last name the middle border is twice as thick.

What I am looking to do is either a: remove the middle border all together if there are two, or the more likely one is to make the two borders collapse into one.

my php function is

function highlight($haystack, $needle, 
                   $wrap_before = '<span class="text_highlight">', 
                   $wrap_after = "</span>"){
    if($needle == '')
        return $haystack;
    $needle = preg_quote($needle);
    return preg_replace("/({$needle})/i", $wrap_before."$1".$wrap_after, $haystack);
}
  • 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-08T10:35:32+00:00Added an answer on June 8, 2026 at 10:35 am

    If you use the regex /({$needle}+)/i regex will match groups of o’s together along with single o’s. So your modified code would look like:

    function highlight($haystack, $needle, 
                       $wrap_before = '<span class="text_highlight">', 
                       $wrap_after = "</span>"){
        if($needle == '')
            return $haystack;
        $needle = preg_quote($needle);
        return preg_replace("/({$needle}+)/i", $wrap_before."$1".$wrap_after, $haystack);
    }
    

    The + matches one or more of the previous character (or characters from a group).

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

Sidebar

Related Questions

How do I search and replace whole words using sed? Doing sed -i 's/[oldtext]/[newtext]/g'
Im doing a search where the POST is: sok3 = 0,2,6 I would then
The Mongoid documentation only gives one example of doing a wildcard search: Person.where(first_name: /^d/i)
Possible Duplicate: highlighting search results in php/mysql I am doing a search with a
Does the JavaScript regex standard support forcing case when doing a search/replace? I am
I am doing a file search and replace for occurrences of specific words in
What is the fastest way to do a search and replace on a string
I've tried to write a string replace function in C, which works on a
After doing a long search on stackoverflow i didn't find any one talked about
After doing a light search on the Lazarus site I've come to the conclusion

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.