php - Return only matched occurrences with strstr() -
say have following string:
$string = 'great @jason think @sally love too.'
i'm using:
$mentions = strstr($string, '@'); echo $mentions which outputs
@jason think @sally love too. desired output
@jason @sally i can't figure how in php, in js do:
hashtags = /\@\w+/g; var matches = string.match(hashtags); alert(matches); preg_match() in php returns boolean, closest can strstr()... returns whole string after first match.
you can use preg_match_all same regex:
<?php $string = 'great @jason think @sally love too.'; preg_match_all("/@\w+/", $string, $matches); var_dump($matches); ?> demo: http://ideone.com/ngrrvh
Comments
Post a Comment