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

  • SEARCH
  • Home
  • 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 1013907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:10:30+00:00 2026-05-16T10:10:30+00:00

I have a custom php script that looks through text to find key phrases

  • 0

I have a custom php script that looks through text to find key phrases and replaces them using preg_replace, but I’m having trouble getting a clean regex for the job.

For example this sentence:

The fox jumped over the gated fence
because it did not have hands to open
the gate.

I would want to bold all instances of gate, yet respect oddities like gated. And also preserve original capitalization too if possible.

So the end sentence would read like this:

The fox jumped over the gated fence
because it did not have hands to open
the gate.

Here is the regular expression I’ve created myself, and you will see that it would not account for gate. because of the period following the word. And also the regex does not respect any original capitalization.

$description  = preg_replace("/ $keyphrase /", " <b>$keyphrase</b> ", $description, $limit);
  • 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-16T10:10:31+00:00Added an answer on May 16, 2026 at 10:10 am
    preg_replace_callback('/\b'.preg_quote($keyphrase, '/').'\b/i',
        function ($matches) {
            return "<b>" . $matches[0] . "</b>";
        }, htmlspecialchars($subject));
    

    You need to use word boundaries (\b). Do not forget to:

    • Properly escape the key phrase for use in the regex expression (use preg_quote).
    • Properly escape for HTML special characters the sentence. This is necessary assuming you’re turning a text string into an HTML string. If the initial string already has HTML, do not use regex.

    EDIT PHP < 5.3 must use:

    function cb($matches) {
        return "<b>" . $matches[0] . "</b>";
    };
    preg_replace_callback('/\b'.preg_quote($keyphrase, '/').'\b/i', 'cb',
       htmlspecialchars($subject));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a custom PHP/MySQL web application that gets updated and copied (using SFTP)
I have a Highchart scatter chart that plots data calculated using a PHP script.
Hello I have made a PHP script for wordpress that insert new posts using
Given that I have a custom PHP error handler already, does it make sense
I have a custom MVC PHP framework that has a router class, which calls
I have a custom implementation of a RESTful API for a PHP application that
I am using a custom captcha php script along with news letter scripts to
I have created a custom PHP script which uploads information submitted by a form
I have a custom script that outputs a list of particular products in csv
I have a (custom) php framework that contains following structure: app |-classes |-settings core

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.