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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:29:02+00:00 2026-06-04T11:29:02+00:00

I would like to use PHP’s preg_replace() to search a text for occurrences of

  • 0

I would like to use PHP’s preg_replace() to search a text for occurrences of a certain word, and enclose that word in brackets, unless there are already brackets present. The challenge here is that I want to test for brackets that may or may not be directly adjacent to the text I am looking for.

Random example: I want to replace warfarin with [[warfarin]]

  1. in this string: Use warfarin for the prevention of strokes
  2. but not in this string: Use [[warfarin]] for the prevention of strokes (brackets already present)
  3. and not in this string either: Use [[generic warfarin formulation]] for the prevention of strokes (‘remote’ brackets already present)

I can satisfy the first two requirements all right using lookbehind and lookahead assertions:

php > echo preg_replace( "/(?<!\[\[)(warfarin)(?!]])/", "[[$1]]", "Use warfarin for the prevention of strokes" );
Use [[warfarin]] for the prevention of strokes
php > echo preg_replace( "/(?<!\[\[)(warfarin)(?!]])/", "[[$1]]", "Use [[warfarin]] for the prevention of strokes" );
Use [[warfarin]] for the prevention of strokes

But I need your help with the third requirement, i.e. not adding brackets when there are ‘remote’ brackets present:

php > echo preg_replace( "/(?<!\[\[)(warfarin)(?!]])/", "[[$1]]", "Use [[generic warfarin formulation]] for the prevention of strokes" );
Use [[generic [[warfarin]] formulation]] for the prevention of strokes

In this last example, the square brackets should not be added to the word warfarin since it is contained in a longer expression that is already enclosed in brackets.

The problem is that PHP’s regexp assertions must have fixed length, otherwise it would be very simple.

I’m using

PHP 5.3.10-1ubuntu3.1 with Suhosin-Patch (cli) (built: May  4 2012 02:20:36)

Thanks in advance!

  • 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-04T11:29:03+00:00Added an answer on June 4, 2026 at 11:29 am

    This is what I would do.

    $str = 'Use warfarin for the prevention of strokes. ';
    $str .= 'Use [[warfarin]] for the prevention of strokes. ';
    $str .= 'Use [[generic warfarin formulation]] for the prevention of strokes';
    $arr = preg_split('/(\[\[.*?\]\])/',$str,-1,PREG_SPLIT_DELIM_CAPTURE);
    // split the string by [[...]] groups
    for ($i = 0; $i < count($arr); $i+=2) {
        // even indexes will give plain text parts
        $arr[$i] = preg_replace('/(warfarin)/i','[[$1]]',$arr[$i]);
        // enclose necessary ones by double brackets
    }
    echo '<h3>Original:</h3>' . $str;
    $str = implode('',$arr); // finally join them
    echo '<h3>Changed:</h3>' . $str;
    

    will result in

    Original:

    Use warfarin for the prevention of strokes. Use [[warfarin]] for the prevention of strokes. Use [[generic warfarin formulation]] for the prevention of strokes

    Changed:

    Use [[warfarin]] for the prevention of strokes. Use [[warfarin]] for the prevention of strokes. Use [[generic warfarin formulation]] for the prevention of strokes

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

Sidebar

Related Questions

I would like to use PHP to input a certain text and the output
Hello I would like to use preg_match in PHP to parse the Desired text
I have a function that I use on index.php page and I would like
For class, I would like a to use PHP MVC framework very similar to
I would like to change this: // use appropiate lang.xx.php file according to the
I would like to ask if it's possible to use PHP in removing a
How can I use PHP to uniquely identify separate machines. I would like to
For one of my projects I would like to use PHP to read an
I would like to know If I can use a php function on a
I have a situation where I would like to use a php file query.php

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.