Varun Sridharan
Varun Sridharan

Reputation: 1978

find a word containing symbols using php

I am currently developing a simple chat script..

in that i need to use @mensions -- Like (Facebook,Twitter) eg @user2019

i need a php to separate to find the @mensions and remove that from the text

Eg : user types like this " @user2019 hi this is a text message"

i need to search and replace @user2019 with a space and i need to get the username

how its possible using php ?

I tried using the below script

$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

Upvotes: 1

Views: 97

Answers (1)

Theox
Theox

Reputation: 1363

I used a regexp to capture the username, and then to remove the username from the message :

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

Result :

blabla hi this is a text message

$username : user2019

Upvotes: 4

Related Questions