The below matches when there’s excess white space. Now I need it to match regardless of what’s in between it… I tried replacing \s* with .+? but that didn’t work. I’m looking at a regex cheat sheet and can’t figure out the most efficient way to do this.
preg_replace("/<sup>(\s*)$key(\s*)<\/sup>/i", "<sup>$val</sup>", $text);
Should match: <sup>(anything here){$key}(anything here)</sup>
update
This is sad… I just realized the regex I’m working with doesn’t need to match whitespaces, but a <br> tag… something like
$text = preg_replace("/<sup>([\s\n(<br>)]*)$key([\s\n(<br>)]*)<\/sup>/is", "<sup>$val</sup>", $text);
I’m not entirely sure I understand, but if you want
.*?to also match newlines, you need the/smodifier:Be aware though, that this might match more than you think. If you have more than one
<sup>tag, and only the last one contains$key, the regex will match all the way from the first<sup>to the last</sup>! So better be specific about what you want to allow there.I’d suggest you use
(?:(?!</?sup).)*instead of.*?in the above regex. This ensures that you’ll never match across tag boundaries:So, in the end, you get: