Reputation: 980
What is the best way to find @username
in a string?
At first I was exploding the string based on @
's found, but it seemed like a lot of trouble going through each of the instances.
Can I use regex for this to find all @usernames
in a string?
Upvotes: 0
Views: 1360
Reputation: 20540
Yes of course. RegEx is the right way to go:
if (preg_match_all('!@(.+)(?:\s|$)!U', $text, $matches))
$usernames = $matches[1];
else
$usernames = array(); // empty list, no users matched
Upvotes: 2
Reputation: 71
you can use strpos() instead
for references http://php.net/manual/en/function.strpos.php
$pos = strpos($mystring, $findme);
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
Upvotes: 0