Regular expression to find and replace @ mentions like twitter

后端 未结 3 665
一生所求
一生所求 2020-12-18 16:00

I am developing a PHP application which needs a regular expression to replace the @ mentions like twitter. Also the regular expression should satisfy the following needs.

3条回答
  •  眼角桃花
    2020-12-18 16:36

    As twitter can contain up to 15 characters, you could write it like this to avoid some bugs:

    $tweet = preg_replace("/(^\w)@(\w{1,15})/i", "\\1@\\2", $tweet);
    

提交回复
热议问题