jmack549 Posted December 3, 2012 Share Posted December 3, 2012 I am trying to count how many times a certain character shows up in a string, and store the immediate word afterwards into an array. Something like Twitter, it's a project where I have to store "hashtag" words to a "trending" array. Here's what I have now: if (strpos($message,'#') !== false) { $numberofhashtags = substr_count($message, '#'); if($numberofhashtags > 1) { $i = 0; while($numberofhashtags>$i) { $trendingwords[$i]=strstr($message, '#'); echo $trendingwords[$i] . "</br>"; $i++; } } else { $trendingwords = strstr($message, '#'); $trending="INSERT INTO `trending` (`word`, `count`) VALUES('$trendingwords', '0')"; mysql_query($trending) or die(mysql_error()); } I obviously can't figure the loop portion out though, because when the strstr it takes EVERYTHING after the #, not just the next word. So it's taking the full sentence. I can't find any other solution that will work for this. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/271545-storing-several-words-after-a-character-to-array/ Share on other sites More sharing options...
requinix Posted December 3, 2012 Share Posted December 3, 2012 One word: regex. preg_match_all('/(?<=#)\w+/', $message, $matches); Do a print_r($matches) to see what the output looks like. Quote Link to comment https://forums.phpfreaks.com/topic/271545-storing-several-words-after-a-character-to-array/#findComment-1397245 Share on other sites More sharing options...
jmack549 Posted December 3, 2012 Author Share Posted December 3, 2012 One word: regex. preg_match_all('/(?<=#)\w+/', $message, $matches); Do a print_r($matches) to see what the output looks like. Perfect, thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/271545-storing-several-words-after-a-character-to-array/#findComment-1397281 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.