2012-02-05 65 views
-1

在數據庫中有很多推文,我想用RT替換RT中的RT @username:... rest of tweet,但顯然只有在「 RT「代表推文的開頭。 (函數應該是不區分大小寫)每當字符串以「RT」開頭時,標籤

所以:輸入將是例如

RT @username I'm having one jolly good time!! 
RT @username RT @username this is fun 
I am not using RT as a label - I use my own "retweet" label :P 
rt @name here rt is lowercase 

輸出:

[RT] @username I'm having one jolly good time!! 
[RT] @username RT @username this is fun 
I am not using RT as a label - I use my own "retweet" label :P (no changes) 
[RT] @name here rt is lowercase 

這是對正則表達式或常規PHP工作呢?任何提示將不勝感激!

回答

2

我用正則表達式做到這一點:

$result = preg_replace("/^RT/uim", "[RT]", $searchText); 

如果你輸入的字符串是不是多,你可以刪除m

0

沒有必要的PHP。一步完成,直接在mySQL中完成。

UPDATE 
    tweet_table 
SET 
    tweet_text = '[RT] ' + SUBSTRING(tweet_text, 4) 
WHERE 
    SUBSTRING(tweet_text, 1, 3) = 'RT '