Retrieve all hashtags from a tweet in a PHP function

I created my own solution. It does:

  • Finds all hashtags in a string
  • Removes duplicate ones
  • Sorts hashtags regarding to count of the existence in text
  • 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
)

Leave a Comment