I have a string that looks like this:
Submission 1052 (topic) was deleted by user username
After this string, there could be a colon followed by a reason for the removal. Now, I need a backreference in PHP’s preg_replace to replace this the way I want it (in my case, I link the username). I’ve tried quite a few expressions, but I can’t seem to get the results I want. Mostly my expressions results in that the link continues on after the colon (I want the link to stop right before the colon).
The regular expression I’m using now looks like this:
<?php preg_replace("/Submission ([0-9]+) (\(.*\)) was deleted by user (.*)(|: .*)/",'<span style="color:red;">Submission <a href="#">$1</a> $2 was deleted by user <a href="#">$3</a>$4</span>',$string) ?>
Here, I basically thought that (|: .*) would check for an empty string OR (|) a colon followed by anything (but I think I’m dead wrong).
Edit Based upon your comment.
I’ve put a
?after the*which makes it non-greedy. As it was, it was grabbing everything to the end of the line.Explanation: