Pr0no
Pr0no

Reputation: 4109

Update regex to only match capital characters in a single word

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?

Upvotes: 0

Views: 82

Answers (3)

busypeoples
busypeoples

Reputation: 737

return preg_replace("/\bRT\b/", $tag, $tweet);

Upvotes: 1

zerkms
zerkms

Reputation: 255155

preg_replace("/RT(?=\s)/", $tag, $tweet);

http://ideone.com/P7dxm

Upvotes: 1

iDifferent
iDifferent

Reputation: 2210

You are making it case insensitive by adding i in the regex modifier, remove it to match only the capital RT.

Upvotes: 1

Related Questions