Consider the following string
$input = "string with {LABELS} between brackets {HERE} and {HERE}";
I want to temporarily remove all labels (= whatever is between curly braces) so that an operation can be performed on the rest of the string:
$string = "string with between brackets and";
For arguments sake, the operation is concatenate every word that starts with ‘b’ with the word ‘yes’.
function operate($string) {
$words = explode(' ', $string);
foreach ($words as $word) {
$output[] = (strpos($word, 0, 1) == 'b') ? "yes$word" : $word;
}
return implode(' ', $output);
}
The output of this function would be
"string with yesbetween yesbrackets and"
Now I want to insert the temporarily deleted labels back into place:
"string with {LABELS} yesbetween yesbrackets {HERE} and {HERE}"
My question is: how can I accomplish this? Important: I am not able to alter operate(), so the solution should contain a wrapper function around operate() or something. I have been thinking about this for quite a while now, but am confused as to how to do this. Could you help me out?
Edit: it would be too much to put the actual operate() in this post. It will not really add value (except make the post longer). There is not much difference between the output of operate() here and the real one. I will be able to translate any ideas from here, to the real-world situation 🙂
The answer to this depends on wether or not you are able to understand
operate(), even if you can’t change it.If you have absolutely no insight into
operate(), your problem is simply unsolvable: To reinsert your labels you need one ofoperate())operate()will work on them)If you have at least some insight into
operate(), this becomes something between solvable and easy:operate($a . $b)==operate($a) . operate($b), then you just split your original input by the labels, run the non-label parts throughoperate(), but obviously not the labels, then reassembleoperate()is guaranteed to let a placeholder string, that itself is guaranteed to be not part of the normal input (“\0” and friends come to mind) alone, then you extract your labels in order, replace them by the placeholder, run the result throughoperate()and later replace the placeholder by your saved labels (in order)Edit
After reading your comments, here are some lines of code