I have what seems a simple enough regex problem that I can’t seem to figure out!
This is the expression I start with:
## a ##
text1
## b ##
text2
## c ##
text3
This is what I want to finish with:
<text title="a">
text1
</text>
<text title="b">
text2
</text>
<text title="c">
text3
</text>
So far this is what I have tried:
preg_replace ('/##(.*?)##(.*?)##/s', '<text title="$1">$2</text>==', $data);
The issue I’m having is that preg_replace restarts its searching from the end of the last match, is there any way to change this?
Alternatively if my strategy is terrible, what is the best way to do this?
Your problem is that the last
##in the expression gets consumed by the first match, so it will not match the next match.You can use a lookahead to avoid that. Like so:
But you probably want something more like: