Return only matched occurrences with strstr()
Say I have the following string:
$string = 'Great @jason I think @sally would love it too.'
$mentions = strstr($string, '@'); echo $mentions
@jason I think @sally would love it too.
I just can't figure how to do this in php, in js I simply do:
hashtags = /\@\w+/g; var matches = string.match(hashtags); alert(matches);
preg_match() in php returns a boolean, so the closest I can get is with strstr()... but returns the whole string after the first match.
You can use preg_match_all with that same regex:
<?php $string = 'Great @jason I think @sally would love it too.'; preg_match_all("/@\w+/", $string, $matches); var_dump($matches); ?>