I am using preg_match_all to search for HashTag values in a Twitter Search response.
It works as I expected except for when the search results don’t have any hash values in them. For some reason my $tags array still has values and I’m not sure why.
Is it because my RegEx is not correct, or is it a problem with preg_match_all?
Thanks
$tweet = "Microsoft Pivot got Runner-Up for Network Tech from The Wall Street Journal in 2010 Technology Innovation Awards http://bit.ly/9pCbTh";
private function getHashTags($tweet){
$tags = array();
preg_match_all("/(#\w+)/", $tweet, $tags);
return $tags;
}
results in:
Array ( [0] => Array ( ) [1] => Array ( ) )
Expected results:
Array();
In default mode,
preg_match_allreturns an array of matches and submatches:So in this case the first array is the array of matches of the whole pattern and the second array is the array of matches of the first subpattern. And since there was no match found, both arrays are empty.
If you want the other order, having each match in an array with its submatches, use
PREG_SET_ORDERin the flags parameter: