I need to capture a certain pattern multiple times while also remembering what’s before, after and in between. For example:
some text “to be captured” some more text “to be captured” some more
text
The only things that are predictable are the tokens that delimit the text to be captured. The captured text itself is different every time. In the end I need to place css spans around those captured parts, like so
some text <span class="a">"to be captured"</span> some more text <span
class="a">"to be captured"</span> some more text
I tried
if (preg_match("/(.*?)(\".*?\")(.*)/", $line, $m)
$res .= $m[1] . '<span class="a">' . $m[2] . '</span>' . $m[3];
It works for a line with only one capture. Using preg_match_all doesn’t fix this, probably I will also have to change the regex itself, but I don’t know how.
Did you try preg_replace?
ps: I’am still not sure what is the problem of OP, without examples. If you have a set of delimiters then regexp could be