Add (^|[ ]) before RT to your regular expression to match the beginning of a line or space. Add more characters between the square brackets to include them (for example, (^|[ _]) to also match underscores.
Explanation
^ matches start of line[ ] matches a space (U + 0020) (or any other character between [ and ] )( i ) make a group| between ( ) means or
So...
(^|[ ]) means a group that is either the beginning of a line or a space (U + 0020)
New regex
echo preg_replace("/(^|[ ])(\RT(?=\s))/", '$1{RT}', $tweet);
Note. . @DVK mentioned that bad practice only matches the beginning of a line and a space (and not word boundaries). Since specific characters were requested by the OP, word-matching is not technically correct. However, since @DVK really made a mistake, I would like to mention that using (\b) instead of (^|[ ]) in many cases will give results that are better suited to your idea “correctly” (for example, “Awesome, RT Some tweet ".). However, keep in mind that this note was added after adoption and is in no way part of the answer to this specific question - it is provided only to those who may encounter this answer for a similar but different problem.
0b10011
source share