Consider the following:
$tweet = "RT @forunemagazine Comment here RT @foo Blah";
function _process_retweets($tweet) {
preg_match('/RT +@[^ :]+:?(.*)/ui', $tweet, $retweets);
if (count($retweets) > 0) {
$tag = ' {RT} '; // In reality, output could also be {RT|id}, etc.
// but this is not relevant here
return preg_replace("/RT/ui", $tag, $tweet);
}
else {
return $tweet;
}
}
echo _process_retweets($tweet);
The expected output here is:
{RT} @fortunemagazine Comment here {RT} @foo Blah
However, because there is a "rt" in @fortunemagazine, the output is:
{RT} @fo {RT} unemagazine Comment here {RT} @foo Blah
I thought the regex was covered for mistakes like this. It sould only match exactly RT, which could be at the beginning of the string: "RT @UserName" or in the middle somewhere: "... RT @UserName ..." but always in CAPITALS and never is a valid "RT" followed by any other character than a space and then a "@username", in which "username" can be a-zA-Z_
What am I doing wring in the regex?