使用 PHP 查找包含符号的单词


find a word containing symbols using php

我目前正在开发一个简单的聊天脚本。

因为我需要使用@mensions - 喜欢(Facebook,Twitter),例如@user2019

我需要一个 PHP 来分离以查找@mensions并将其从文本中删除

例如:像这样的用户类型" @user2019 hi this is a text message"

我需要搜索并用空格替换@user2019,我需要获取用户名

如何使用PHP?

我尝试使用以下脚本

$email  = 'name@example.com';
$domain = strstr($email, '@');
echo $domain."<br/>"; // prints @example.com
$user = strstr($email, '@', true); // As of PHP 5.3.0
echo $user."<br/>"; // prints name

我使用正则表达式来捕获用户名,然后从消息中删除用户名:

$string = "blabla @user2019 hi this is a text message";
$pattern = '/(.*?)@'w+(.*?)/i';
$replacement = '${1}';
$getuser = preg_match('/@'w+/', $string, $matches);
$username = str_replace('@', '', $matches[0]);
echo preg_replace($pattern, $replacement, $string);

结果:

布拉布拉 嗨,这是一条短信

$username : 用户2019