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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:04:23+00:00 2026-06-14T13:04:23+00:00

I am experimenting a problem with | (or) and the regex in php. Here

  • 0

I am experimenting a problem with | (or) and the regex in php.

Here is what I would like to do, for example I have those 3 sentences :

“apple is good to eat”

“an apple a day keeps the doctor away”

“i like to eat apple”

And lets say that i would like to change the word apple by orange, so here is my code :

$oldWord="apple";
$newWord="orange";
$text = preg_replace('#^' . $oldWord . ' #', $newWord, $text);
$text = preg_replace('# ' . $oldWord . ' #', $newWord, $text);
$text = preg_replace('# ' . $oldWord . '$#', $newWord, $text);

Of course it works but i haven’t found the right combinaison to do that with only one line of code with the key word | (or).

Do you guys have any suggestion please ? thanks

  • 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-14T13:04:25+00:00Added an answer on June 14, 2026 at 1:04 pm

    Note that your regex removes the spaces around apple. If this is not what you desire, but instead you just want to replace apple if it’s the full word, then, as some others recommended, use word boundaries:

    $text = preg_replace('#\b' . $oldWord . '\b#', $newWord, $text);
    

    If you did intent to remove the spaces, too you could require a wordboundary, but make the spaces optional:

    $text = preg_replace('#[ ]?\b' . $oldWord . '\b[ ]?#', $newWord, $text);
    

    If they are there, they will be removed, too. If they are not, the regex doesn’t care. Note that [ ] is completely equivalent to just typing a space, but I find it more readable in a regex.

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

Sidebar

Related Questions

Bit of an abstract problem here. I'm experimenting with the Domain Model pattern, and
i'm experimenting pygtk programms development, with python 2.7, and i have a little problem
I´m experimenting a bit with css sprites and have a small problem wich I
Update I found the cause of the problem. I have been experimenting with the
im new to programming, i have been experimenting with cocos2d, heres the problem, i
I have been experimenting with the Change HUE/Saturation example found on the EFG website:
I'm experimenting with jQuery Mobile and have run into a problem on my Windows
Im experimenting a bit with jquery and having a big problem which i have
I've started experimenting with Haskell and have a problem. qqq is a function that
I'm experimenting with WCF Services, and have come across a problem with passing Interfaces.

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.