I want to retrieve all hashtags from a tweet using a PHP function.
I know someone asked a similar question here, but there is no hint how exactly to implement this in PHP. Since I'm not very familiar with regular expressions, don't know how to write a function that returns an array of all hashtags in a tweet.
So how do I do this, using the following regular expression:
#\S*\w
I created my own solution. It does:
Supports unicode characters
function getHashtags($string) { $hashtags= FALSE; preg_match_all("/(#\w+)/u", $string, $matches); if ($matches) { $hashtagsArray = array_count_values($matches[0]); $hashtags = array_keys($hashtagsArray); } return $hashtags; }
Output is like this:
( [0] => #_ƒOllOw_ [1] => #FF [2] => #neslitükendi [3] => #F_0_L_L_O_W_ [4] => #takipedeğerdost [5] => #GönüldenTakipleşiyorum )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With