Regular expression to find and replace @ mentions like twitter

后端 未结 3 664
一生所求
一生所求 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:28

    Wow. I found the answer myself guys.

    $tweet = preg_replace('/(^|[^a-z0-9_])@([a-z0-9_]+)/i', '$1@$2', $tweet);
    

    Thanks for your help guys.

提交回复
热议问题