Check the word after a '@' character in PHP

前端 未结 4 1679
我在风中等你
我在风中等你 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:42

    Looks like a job for preg_replace_callback():

    $string = preg_replace_callback('/@([a-z0-9_]+)/', function ($matches) {
      if ($user = get_user_by_username(substr($matches[0], 1)))
        return ''.$user['name'].'';
      else
        return $matches[0];
    }, $string);
    

提交回复
热议问题