regex - How to get the words that start with '#' in string using php? -
this question has answer here:
- retrieve hashtags tweet in php function 6 answers
i have sentence
"my name's #jeff,what thy name?" and want #jeff sentence. have tried code
for ($i=0; $i <=substr_count($text,'#'); $i++) { $a=explode('#', $text); echo $a[$i]; } but returns #jeff,what thy name? not soul desires
there simpler solution it. use preg_match() find target part of string using regex.
preg_match("/#\w+/", $str, $matches); echo $matches[0] check result of code in demo
if want matches string, use preg_match_all() find matches.
preg_match_all("/#\w+/", $str, $matches); print_r($matches[0]);
Comments
Post a Comment