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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:39:26+00:00 2026-06-04T17:39:26+00:00

I generated the following regex code with http://gskinner.com/RegExr/ where it works, but when I

  • 0

I generated the following regex code with http://gskinner.com/RegExr/ where it works, but when I execute it with PHP, it fails to use the match in the replacement string.

preg_replace(
    '/(?<=\>)\b\w*\b|^\w*\b/',
    '<b>$&</b>',
    'an example'
);

Output:

<b>$&</b> example

Expected:

<b>an</b> example

I know that obviously the $& is not doing the correct thing, but how can I get it to work?

  • 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-04T17:39:28+00:00Added an answer on June 4, 2026 at 5:39 pm

    Try with this instead

    preg_replace('/(?<=\>)\b\w*\b|^\w*\b/', '<b>$0</b>', $string);
    

    $0 means it will become the first thing matched in your regex, $1 will become the second etc.

    You could also use back-references; \0 gets the first thing matched back from where you are, \1 gets the second thing matched back etc. More Info

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

Sidebar

Related Questions

Take the following JSON string (generated by some ExtJS code - but that's irrelevant):
I have the following XML: http://pastebin.com/QiRK72BK which is generated in response to a REST
I have the following Regex to match all link tags on a page generated
I'm confused about the errors generated by the following code . In Derived::doStuff, I
I am using the following code to minify html generated from aspx page duuring
rake db:schema:dump rake db:test:prepare All use the following generated schema.rb: t.column cost_per_license, :decimal, :limit
I were asked to analyze an assembly code, which was generated from following c++
I have generated the following plot using the R code that follows it: ggplot(lengths,
I am trying to rewrite the following URL in WP: http://www.example.com/?compra=arriendo&propertytype=apartamentos&location=bogota&habitaciones=2-habitaciones to: http://www.viveya.co/arriendo/apartamentos/bogota/2-habitaciones This
I would like to use the following code to restrict input (to alphanumeric) into

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.