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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:18:57+00:00 2026-05-30T08:18:57+00:00

I need to bold a search term and its context (within a sentence). Consider

  • 0

I need to bold a search term and its context (within a sentence).

Consider the string:

Lorem ipsum dolor sit amet. Consectetuer adipiscing elit.

If the search term is Lorem ipsum, then the result should be:

Lorem ipsum dolor sit amet. Consectetuer adipiscing elit.

If the search is dolor sit, the result should be:

Lorem ipsum dolor sit amet. Consectetuer adipiscing elit.

If the search is Consectetuer, the result should be:

Lorem ipsum dolor sit amet. Consectetuer adipiscing elit.

How do you recommend I do this (in php)?

Bolding the search term is easy enough:

$string = str_replace($query,'<strong>' . $query . '</strong>', $string);

But how can I include two words before and after without overlapping to the next or previous sentence?

  • 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-30T08:18:59+00:00Added an answer on May 30, 2026 at 8:18 am

    You could split the string into “sentences” (split on a full stop (what about exclamation marks, question marks, …)).

    Then find the sentence with the matching word.

    Then split that sentence into “words” and add some tags in two words before and after the matching word. Since you only have one sentence to work on, you’d include a check to make sure you didn’t go out of bounds of the word array.

    Then join the words back together, and join all the sentences back together.


    Alternatively, you could use regex and preg_replace (although that may not be a road you want to go down, especially if you have an option such as splitting on plaintext – there’s a quote that goes something like “You have a problem and you want to use regex for it. Now you have two problems.”):

    $string = preg_replace("/\\b(\\w+ +){0,2}$query( +\\w+){0,2}\\b/i",
                           '<strong>$0</strong>',
                           $string);
    

    The regex works like this (backslashes are escaped above):

    \b        | match a word boundary (ie match whole words)
    (\w+ +)   | match a "word" followed by spaces (to separate it from the next word)
    {0,2}     | match 0 to 2 of these such words (it will match as many as possible
              | up to 2)
    $query    | match the '$query' string
    ( +\w+)   | regex for space (separating $query) followed by a word
    {0,2}     | match 0 to 2 of these words (as many as possible up to 2)
    \b        | match a word boundary (ie match whole words)
    

    The /i at the end means “case insensitive”.

    The replacement string, <strong>$0</strong>, means to replace with all the matched words surrounded by the ‘strong’ tags.

    The reason this works is that the regular expression does not allow a full stop to be matched. So it will grab up to 2 words either side of $query but is forbidden from going over full stops.

    There are the usual caveats (that you would have with any method you used) — do you want the bolding to go over question marks? exclamation marks? Is an apostrophe allowed in a word? What will you do about non-full-stop punctuation between words? etc.

    I’d recommend refining the above regex (if you want to use regex, that is) to:

    • allow apostrophes in a word: change \w+ to [\w']+ (escape your for PHP backslashes too)
    • allow various punctuation symbols between words: change the + to something like [\s\-&,]+ (means “space”, “-“, “&”, “,” are allowed between words — add more to your liking, but don’t put “.” in to prevent the bold-ing from going over full stops).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a string message i need first word of that as bold
I need to convert a string like this: A &#039;quote&#039; is <b>bold</b> into: A
I need to change the font style from bold to italic,by using two buttons.for
I like Notepad++, but sometimes I need some words set in bold or cursive,
I need to use a regexp to extract bold text from rtf formatted text.
I need to set my NSTextField text as bold using xib.Pleas help !
I need a small FAQ page with Question in bold font and answer in
I am using ordinary search functionality in my business controller . but now need
I have a large HTML document, a book, and need to bold every occurrence
I'm using Prawn and need to make the Header row bold but I can't

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.