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?
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.
Regex solution: