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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:34:48+00:00 2026-05-25T23:34:48+00:00

I have problems with pattern. I need to process some text for example: <div>{text1}</div><span>{text2}</span>

  • 0

I have problems with pattern.
I need to process some text for example:

<div>{text1}</div><span>{text2}</span>

After the process I need to get in place of {text1} and {text2} some words from dictionary.
I’m preparing ui interface for diffrent languages.
This is in PHP. For now I have something like this but this doesn’t work:

function translate_callback($matches) {
  global $dict;
  print_r($matches);
  return 'replacement';
}

function translate($input) {
  return preg_replace_callback('/(\{.+\})/', 'translate_callback', $input);
}

echo translate('<div>{bodynum}</div><span>{innernum}</span>');

This is test scenarion but I can’t find the way to define pattern because this one in code match

{bodynum}</div><span>{innernum}

but I want pattern that will match

{bodynum} and then {innernum}

Can somebody help me with this. Thanks.

The Solution:

To match any character between { and }, but don’t be greedy – match the shortest string which ends with } (the ? stops + being greedy).

So the pattern now looks like: ‘/{(.+?)}/’ and is exacly what I want.

  • 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-05-25T23:34:48+00:00Added an answer on May 25, 2026 at 11:34 pm

    As you correctly note in the comments, you can use an ungreedy match to stop at the first closing brace rather than the last:

    preg_replace_callback('/(\{.+?\})/', 'translate_callback', $input);
    

    As Damien notes, you can also use the /U modifier to make all matches ungreedy by default.

    Alternatively, you could restrict the set of characters allowed between the braces to some set that does not include }:

    preg_replace_callback('/(\{[^}]+\})/', 'translate_callback', $input);
    

    or even:

    preg_replace_callback('/(\{\w+\})/', 'translate_callback', $input);
    

    which allows only word characters between the braces. This might (or might not) be slightly more efficient than using ungreedy matching, due to the way the PCRE engine works. In practice, of course, there’s unlikely to be any observable difference.

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

Sidebar

Related Questions

I have problems to get rspec running properly to test validates_inclusion_of my migration looks
I have problems getting some of my views aligned in a relative layout that
I have a series of large, flat text files that I need to parse
I have some problems with IntelliJ Idea not copying SQL resource files into target
I have a question regarding WPF UserControls and MVVM Pattern, I need to expose
So, one problem pattern that I keep coming across and don't have a good
I have a problem and I want to implement the MVC pattern to my
I have problems with bringing a windows mobile 6 form to the front. I
I have problems binding both a telerik RadGrid and a plain vanilla ASP.NET GridView
I have problems with how I should structure my product listing pages, products pages,

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.