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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:14:40+00:00 2026-05-10T20:14:40+00:00

PHP (among others) will execute the deepest function first, working its way out. For

  • 0

PHP (among others) will execute the deepest function first, working its way out. For example,

$text = strtoupper(str_replace('_', ' ', file_get_contents('file.txt'))); 

I’m doing something very similar to the above example for a template parser. It looks for the tags

{@tag_name} 

and replaces it with a variable of the name $tag_name. One more example:

$a = 'hello'; $b = ' world'; INPUT = 'This is my test {@a{@b}} string.'; OUTPUT (step 1) = 'This is my test {@a} world string.'; OUTPUT output = 'This is my test hello world string.'; 

How can I go about doing this? Does this make sense? If not, I can try explaining better.

  • 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. 2026-05-10T20:14:40+00:00Added an answer on May 10, 2026 at 8:14 pm

    I’m not sure I understand the nesting in your example, as the example doesn’t demonstrate a purpose behind nesting. Your example input could very easily be

    'This is my test {@a} {@b} string.' 

    And using arrays in str_replace would handle this very simply and quickly.

    $aVars = array('{@a}' => 'hello', '{@b}' => 'world'); $sString = 'This is my test {@a} {@b} string.';  echo str_replace(array_keys($aVars), array_values($aVars), $sString); 

    Which gives us

    This is my test hello world string.

    Now, a recursive function for this isn’t too difficult, though I’m not sure I understand how useful it would be. Here’s a working example:

    function template($sText, $aVars) {     if (preg_match_all('/({@([^{}]+)})/',                        $sText, $aMatches, PREG_SET_ORDER)) {         foreach($aMatches as $aMatch) {             echo '<pre>' . print_r($aMatches, 1) . '</pre>';              if (array_key_exists($aMatch[2], $aVars)) {                 // replace the guy inside                 $sText = str_replace($aMatch[1], $aVars[$aMatch[2]], $sText);                  // now run through the text again since we have new variables                 $sText = template($sText, $aVars);             }         }     }      return $sText; } 

    That print_r will show you what the matches look like so you can follow the function through its paces. Now lets try it out…

    $aVars = array('a' => 'hello', 'b' => 'world'); $sStringOne = 'This is my test {@a} {@b} string.'; $sStringTwo = 'This is my test {@a{@b}} string.';  echo template($sStringOne, $aVars) . '<br />'; 

    First Result:

    This is my test hello world string.

    Now lets try String Two

    echo template($sStringTwo, $aVars) . '<br />'; 

    Second Result:

    This is my test {@aworld} string.

    That may very well be what you’re looking for. Obviously you would need an aworld variable for this to work recursively…

    $aVars = array('a' => '', 'b' => '2', 'a2' => 'hello world');  echo template($sStringTwo, $aVars) . '<br />'; 

    And our result.

    This is my test hello world string.

    And just for some fun with the recursion…

    $aVars = array('a3' => 'hello world', 'b2' => '3', 'c1' => '2', 'd' => '1'); $sStringTre = 'This is my test {@a{@b{@c{@d}}}} string.';  echo template($sStringTre, $aVars) . '<br />'; 

    This is my test hello world string.

    Not sure if this is what you’re asking for…

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

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If its ThreadStatic there's one copy per thread. So, by… May 12, 2026 at 1:08 am
  • Editorial Team
    Editorial Team added an answer Not sure if this is exactly what you are after… May 12, 2026 at 1:08 am
  • Editorial Team
    Editorial Team added an answer It looks like the thread module became obsolete in 3.x… May 12, 2026 at 1:08 am

Related Questions

I'm researching the idea of building a super-small (preferably PHP) web app, which will
I used to use the standard mysql_connect(), mysql_query(), etc statements for doing MySQL stuff
I am trying to make a few php scripts that will run among other
Writing a small app that ( among other things ) lets users upload a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.