2014-04-07 29 views
1

我目前正在開發一個簡單的聊天腳本符號的字..在我需要使用@mensions發現含有使用PHP

- 贊(Facebook,微博)如@user2019

我需要一個PHP來單獨找到@mensions並從文本刪除

例如:用戶類型,如本" @user2019 hi this is a text message"

我需要用SP搜索和替換@user2019王牌和我需要得到的用戶名

如何使用PHP的可能?

我嘗試使用以下腳本

$email = '[email protected]'; 
$domain = strstr($email, '@'); 

echo $domain."<br/>"; // prints @example.com 

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

「*我需要搜索,並用空格代替@ user2019,我需要獲取用戶名*」 - 你能澄清?你試圖達到的最終結果是什麼? –

+0

@AmalMurali我需要分開字符串並將其保存在一個單獨的變量中 –

回答

4

我使用的正則表達式來捕獲用戶名,然後以除去來自該消息的用戶名:

$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); 

結果:

blabla嗨,這是一條短信

$用戶名:user2019