Check the word after a '@' character in PHP

前端 未结 4 1752
我在风中等你
我在风中等你 2021-02-20 03:25

I\'m making a news and comment system at the moment, but I\'m stuck at one part for a while now. I want users to be able to refer to other players on the twitter style like

4条回答
  •  眼角桃花
    2021-02-20 03:58

    Here's an expression that'll match what you need, but won't capture email addresses:

    $str = '@foo I loved the article, @SantaClaus, thanks for writing to my@email.com';
    preg_match_all('/(^|[^a-z0-9_])(@[a-z0-9_]+)/i', $str, $matches);
    //$matches[2][0] => @foo
    ///$matches[2][1] => @SantaClause
    

    As you can see: my@email.com isn't captured, but the @foo and @SantaClaus strings are...

提交回复
热议问题