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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:32:53+00:00 2026-06-09T17:32:53+00:00

I have a text that has the possible values already in the text, i

  • 0

I have a text that has the possible values already in the text, i want to show the right values in situations. I’m not really good with regexes and i don’t really know how to explain my problem so here is an example. I’ve got it working almost:

$string = "This [was a|is the] test!";

preg_replace('/\[(.*)\|(.*)\]/', '$1', $string);
// results in "This was a text!"

preg_replace('/\[(.*)\|(.*)\]/', '$2', $string);
// results in "This is the test!"

This works without problems but when there are two parts it doesn’t work anymore because it gets the end bracket from the last.

$string = "This [was a|is the] so this is [bullshit|filler] text";

preg_replace('/\[(.*)\|(.*)\]/', '$1', $string);
//results in "This was a|is the] test so this is [bullshit text"

preg_replace('/\[(.*)\|(.*)\]/', '$2', $string);
//results in "This filler text"

Situation 1 should be the values between ( and | and situation 2 should show the values between | and ).

  • 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-09T17:32:54+00:00Added an answer on June 9, 2026 at 5:32 pm

    Your probem is the regex greediness. Add a ? after .* to make it consume only the string within the square brackets:

     preg_replace('/\[(.*?)\|(.*?)\]/', '$1', $string);
    

    Likewise could you use the /U ungreedy modifier. Better yet use a more specific match in place of .*? anything.

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

Sidebar

Related Questions

I have some text that has HTML hyper-links in it. I want to remove
I have some html that creates a dropdown list. The list has text values
I have a text that has multiple occurrences of variables in this format: Example
I have a text string that has been doctored to be web safe URLs
I have a text file that has item numbers in it (one per line).
I currently have a text file that has the following: 1 Commercial & Enterprise
Let's say I have some original text: here is some text that has a
I have a standard text input that has a blur event. The blur event
I have a C# form that has a text box that needs to constantly
I have a text area in a form that has a character limit and

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.