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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:03:12+00:00 2026-06-13T01:03:12+00:00

I’m making a template system whereby I can type: <t:category:item:parameter1:parameter2…> And have it be

  • 0

I’m making a template system whereby I can type:

<t:category:item:parameter1:parameter2...>

And have it be replaced with text from a file. The optional parameters are placed in the replacement string as %1, %2…

So far I have this:

$data = preg_replace_callback("/<t:([^:]+):([^>]+)>/",function($m) use (&$userdata) {
    static $fcache = Array();
    $parse = function($file) use (&$fcache,&$lang) {
        // parse the file if it's a new one. False indicates success, otherwise error message is returned
        if( !isset($fcache[$file])) {
            if( !file_exists("text/".$lang."/".$file.".html")) $lang = "en";
            if( !file_exists("text/".$lang."/".$file.".html")) return "<div class=\"alert\">ERROR: File ".$file." not found.</div>";
            $k = "";
            foreach(file("text/".$lang."/".$file.".html") as $l) {
                if( substr($l,0,1) == "|") $k = rtrim(substr($l,1));
                else $fcache[$file][$k] .= $l;
            }
        }
        return false;
    };
    $lang = $userdata && $userdata['language'] ? $userdata['language'] : "uk";
    list(,$file,$d) = $m;
    $params = explode(":",$d);
    $section = array_shift($params);
    if( $e = $parse($file)) return $e;
    if( !$fcache[$file][$section]) {
        $lang = "uk";
        if( $e = $parse($file)) return $e;
    }
    return preg_replace_callback("/%(\d+)/",function($i) use ($params) {
        return htmlspecialchars_decode($params[$i[1]-1]);
    },trim($fcache[$file][$section]));
},$data);

The format of the text file is:

|key
replacement text
|otherkey
more text %1

Anyway, getting to the point: What if one of the parameters is itself a replacement string? For instance, what if I want a string like “Come and visit him soon!” – I’d like to have it be something like:

<t:person:visit:<t:grammar:pronoun_object_m>>

And the file would have:

|visit
Come and visit %1 soon!

|pronoun_object_m
him

However, the current function will take the parameter as the literal <t:grammar:pronoun_object_m and there would be an extra > showing up at the end of the phrase:

Come and visit <t:grammar:pronoun_object_m soon!>

Which would actually show up as:

Come and visit

due to the unparsed replacement looking like an HTML tag…

I’m fairly sure I need a recursive regex, however I am very confused as to how they work. Could anyone please explain how I could “recursify” my regex to allow for embedded parameters like this?

  • 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-13T01:03:13+00:00Added an answer on June 13, 2026 at 1:03 am

    The problem with recursive solutions is, they don’t work really well with preg_replace. They are mostly intended for preg_match. The reason is that you will only be able to access the last (innermost) capturing of a pattern that is reused within the recursion. So even preg_replace_callback will not help much here.

    Here is another possibility:

    In <t:person:visit:<t:grammar:pronoun_object_m>>, the reason you get the output you mentioned is, that your regex will match this:

    <t:person:visit:<t:grammar:pronoun_object_m>
    

    (It cannot go further, because you disallow > within your placeholders.)

    There are a few ways to get around this. For starters you could also disallow < (and not only >) within your placeholders:

    "/<t:([^:]+):([^<>]+)>/"
    

    Now your pattern will always only find the innermost placeholders. So you could simply call your preg_replace_callback repeatedly until no more replacements are done. How to find this out? Add the optional fourth and fifth parameter:

    do
    {
        preg_replace_callback("/<t:([^:]+):([^<>]+)>/", $function, $data, -1, $count);
    } while($count);
    

    I also suggest (for legibility) that you define the callback outside of the preg_replace_callback function.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.