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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:48:02+00:00 2026-06-14T12:48:02+00:00

I can highlight one word in string like this: $str = my bird is

  • 0

I can highlight one word in string like this:

$str = "my bird is funny";
     $keyword = "fun";
     $str = preg_replace("/($keyword)/i","<b>$0</b>",$str);

Ant this will give me:
my bird is funny

But how to make this work when keyword is from more than one word for example when

$keyword = "bird fun";

I would like to get this result: my bird is funnny

  • 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-14T12:48:03+00:00Added an answer on June 14, 2026 at 12:48 pm

    One of the most basic concepts of regular expressions is alternation. bird|fun will match either bird or fun. This alternation can easily be generated using implode and explode:

    $keywords = explode(' ', trim($keyword));
    $str = preg_replace('/'.implode('|', $keywords).'/i', '<b>$0</b>', $str);
    

    As pritaeas pointed out, you could also use str_replace:

    $str = preg_replace('/'.str_replace(' ', '|', trim($keywords)).'/i', '<b>$0</b>', $str);
    

    Of course, if you write $keyword yourself and don’t use it anywhere else then write it as a regex right away:

    $keyword = 'bird|fun';
    

    or even

    $keyword = '/bird|fun/i';
    

    This way you don’t need any explode or implode at all.

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

Sidebar

Related Questions

i'm using this code to highlight search keywords: function highlightWords($string, $word) { $string =
How can I highlight exactly one item (particularly a line on the x axis)
I know you can highlight an entire row or a word, but I am
I want to make a uiview that can be aware of highlight status, like
Can some one please highlight what should be the process of incorporating reusable django
Where can I find a control for WinForms that will highlight source code pasted
I saw in one of the jQuery books where you can highlight a column
Can anyone enlighten me to a way I can Highlight the content of an
I'm trying to write a Google chrome extension so that I can highlight text
Google Maps itself can sort of Highlight areas, when you are searching for example

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.