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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:18:00+00:00 2026-06-15T11:18:00+00:00

What I’m trying to do is to replace functions within a user-inserted text (say;

  • 0

What I’m trying to do is to replace “functions” within a user-inserted text (say; a blog post) with certain blocks of html, but use option/value pairs within the “function”. Clear? No?! Thought so 🙂 Here’s an example:

Some text, can be long, may be short, a nice story, or just a comment.
{{function option1="value1" option2="value2"}}
And some more text!
{{function2 option1="value1" option2="value2"}}

In the text, I want to replace and parse the {{function ...}} part. A more concrete example could be:

{{youtube videokey="_VIDEOKEY_"}}

which should be replaced by the youtube embed code:

<iframe width="420" height="315" src="http://www.youtube.com/embed/_VIDEOKEY_" frameborder="0" allowfullscreen></iframe>

For this I want to use the preg_replace_callback() function, so I can have some room to do some calculations on the data/options passed.


The problem: I can get and replace the substring formatted like this ({{ ... }}), and even match the option/value pair, the problem is that I cannot get every single o/v pair in the matches array, only the last one.

I have tried a lot of expressions, one of which I think is closest is:

\{\{\w+([[:space:]]+(([0-9a-zA-Z]+)=\"([0-9a-zA-Z]+)\"))+\}\}

As you can see I try to match:

  1. A string within {{ and }}
  2. In which the first part is a word
  3. Followed by one or more option/value pairs:
    • one or more spaces
    • one or more letters or digits (the option name)
    • the = sign
    • one or more letters or digits, enclosed by " (the option value)

In example the text above will match (using preg_match_all):

array(5) (
    0 => array(2) (
        0 => string(46) "{{function option1="value1" option2="value2"}}"
        1 => string(47) "{{function2 option1="value1" option2="value2"}}"
    )
    1 => array(2) (
        0 => string(17) " option2="value2""
        1 => string(17) " option2="value2""
    )
    2 => array(2) (
        0 => string(16) "option2="value2""
        1 => string(16) "option2="value2""
    )
    3 => array(2) (
        0 => string(7) "option2"
        1 => string(7) "option2"
    )
    4 => array(2) (
        0 => string(6) "value2"
        1 => string(6) "value2"
    )
)

And when using preg_replace_callback with this regular expression of course I receive the same set of matches (in a one-dimensional array that is).


I have this solution, but don’t like it (because it involves a regular expression on a regular expression match, while I think it should be possible to do it in one expression):

$input = ... // see text above
$output = preg_replace_callback('@\{\{\w+([[:space:]]+(([0-9a-zA-Z]+)=\"([0-9a-zA-Z]+)\"))+\}\}@', 'my_replace_function', $input);

function my_replace_function($match) {
    preg_match_all('@([0-9a-zA-Z]+)=\"([0-9a-zA-Z]+)\"@', $match[0], $matches);
    // do something with the $matches
}

Is it even possible to deliver to my callback function an array with ALL option/value pairs, not only the last match, and use that data to parse the string? If so, could you please point me in the right direction?

Basically the question is: can I separate repitious subpatterns in the matches?


—Edit—
The solution proposed above (capturing the whole ‘function’-block, then match the option-value pairs within the matched string) is in fact the solution to this puzzle. For more detail please see the answer of @m.buettner below (the accepted one).

  • 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-15T11:18:01+00:00Added an answer on June 15, 2026 at 11:18 am

    You can’t. Sorry, but it’s that simple. Most regex engines do not support capturing multiple values with a single capturing group. Which is equivalent to say, most regex engine support only a finite and fixed number of captures. .NET is the big exception here. But you are using PCRE – and PCRE will always return the last capture for each group (see here, official citation pending; but google for “PCRE repeated capturing group”, all sources agree). And the number if groups is fixed by the number of parentheses in your pattern. Sometimes there are possible work arounds, where you transform your repeated captures into repeated matches, but I think that is not applicable either in your case.

    So your solution is really the right way to go about it. You match the whole {{...}} block, and then parse out the key-value pairs within the callback separately.

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I need to clean up various Word 'smart' characters in user input, including but
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

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.