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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:57:10+00:00 2026-06-18T07:57:10+00:00

In PHP I have some string-tags (pseudo-code): [TAG_A] = X [TAG_B] = Y [TAG_X]

  • 0

In PHP I have some string-tags (pseudo-code):

[TAG_A]              = X
[TAG_B]              = Y
[TAG_X]              = Z

so when I replace these tags in a string:

[TAG_A] and [TAG_B] are connected with [TAG_[TAG_A]]

it should output:

X and Y are connected with Z

The problem lies in the nested tags. It needs to be recursive, where the inner tags are replaced first. All possible tags (with their values) are stored in a large array.

I would like a replace method that not just uses brute force to replace all tags by using foreach on the tags-array, but in fact only searches for []-pairs in the string, and then looks up the value in the tags-array.

Regexes are not the right way, I think, but what is the most efficient way to do something 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-18T07:57:11+00:00Added an answer on June 18, 2026 at 7:57 am

    Replace the tags in the string then check if new tags come up after the process. Run the code again until there are no more tags to be replaced.

    $string = '[TAG_A] and [TAG_B] are connected with [TAG_[TAG_A]]';
    $search = array(
        '[TAG_A]'   => 'X',
        '[TAG_B]'   => 'Y',
        '[TAG_X]'   => 'Z'
    );
    $continue = true;
    while ($continue) {
        foreach ($search as $find => $replace) {
            $string = str_replace($find, $replace, $string);
        }
        $continue = false;
        foreach ($search as $find => $replace) {
            if (strpos($string, $find) !== false) {
                $continue = true;
                break;
            }
        }
    }
    echo $string; // prints "X and Y are connected with Z"
    

    Regex solution:

    $string = '[TAG_A] and [TAG_B] are connected with [TAG_[TAG_A]]';
    $search = array(
        'TAG_A' => 'X',
        'TAG_B' => 'Y',
        'TAG_X' => 'Z'
    );
    while(preg_match_all('/\[([^\[\]]*?)\]/e', $string, $matches)) {
        $string = preg_replace('/\[([^\[\]]*?)\]/e', '$search["$1"]', $string);
    }
    echo $string;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some problem to get the utf-8 string from PHP using jQuery $.load()
i have a problem using some php functions on a string i get from
I have some PHP code that generates dynamic tables of data on the fly.
I have some PHP code that is designed to make a spreadsheet with formulas
I have some PHP code that generates a calendar and then outputs html to
I have some php files in a server which generate xml code in them.
I have some PHP code which allows me to sort a column into ascending
I have some PHP code which stores whatever is typed in a textbox in
<a href=tags.php onclick=return popitup('tags.php')>tags</a> I have this line of code that runs the javascript
I have a string which looks like: $data = ' Some string [code=cpp] int

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.