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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:03:24+00:00 2026-06-11T19:03:24+00:00

I’m working with an API that returns text in the following format: {I want|I

  • 0

I’m working with an API that returns text in the following format:

{I want|I need} to make this {stupid|awesome|irritating} sentence
{formatting {rapidly|quickly} and random|spin and be random}

Using PHP, I need to format the string like:

"I need to make this irritating sentence formatting quickly";

OR

"I want to make this awesome sentence spin and be random";

from the initial text.

I would have no problem if the curly brackets couldn’t contain another set of curly brackets. Any advice or any piece of code that could help me solve this problem?

  • 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-11T19:03:25+00:00Added an answer on June 11, 2026 at 7:03 pm

    I assumed your source string is like this:

    {I want|I need} to make this {stupid|awesome|irritating}
    sentence formatting {rapidly|quickly} and {random|spin and be random} 
    

    otherwise the brackets are nested and your output example does not match how you put them in source string. Then use preg_match_all() like this:

    preg_match_all("/{.*}/U",$sourceString, $result, 
                          PREG_PATTERN_ORDER|PREG_OFFSET_CAPTURE);
    

    which for your $sourceString would produce:

    array(1) {
      [0]=>
      array(4) {
        [0]=>
        array(2) {
          [0]=>
          string(15) "{I want|I need}"
          [1]=>
          int(0)
        }
        [1]=>
        array(2) {
          [0]=>
          string(27) "{stupid|awesome|irritating}"
          [1]=>
          int(29)
        }
        [2]=>
        array(2) {
          [0]=>
          string(17) "{rapidly|quickly}"
          [1]=>
          int(77)
        }
        [3]=>
        array(2) {
          [0]=>
          string(27) "{random|spin and be random}"
          [1]=>
          int(99)
        }
      }
    }
    

    And you get all items. Then you can process each entry, strip “{” and “}”, explode() on “|” to get array of options to choose from. Then you pick what you want and replace formerly found item with it. Note, I capture offset where matching pattern is found, because you cannot just eventually do str_replace() because I assume you want to be able to use the same entry in many places (i.e. “{this|that} foo {this|that}”. str_replace() would replace both while I think it is not desired. So we got offset in the string, length of string can be easily computed but this is sufficient to do some surgery, and cut off our entry and put replacement. Other, cleaner approach is to use preg_replace_callback() and put all that “logic” in the callback so you could do whole processing in one pass.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.