Extracting Twitter hashtag from string in PHP

13,403

Solution 1

Use preg_match() to identify the hash and capture it to a variable, like so:

$string = 'Tweet #hashtag';
preg_match("/#(\\w+)/", $string, $matches);
$hash = $matches[1];
var_dump( $hash); // Outputs 'hashtag'

Demo

Solution 2

As i understand you are saying that in text/pargraph/post you want to show tag with hash sign(#) like this:- #tag and in url you want to remove # sign because the string after # is not sended to server in request so i have edited your code and try out this:-

$string="www.funnenjoy.com is best #SocialNetworking #website";    
$text=preg_replace('/#(\\w+)/','<a href=/hash/$1>$0</a>',$string);
echo $text; // output will be www.funnenjoy.com is best <a href=search/SocialNetworking>#SocialNetworking</a> <a href=/search/website>#website</a>

Solution 3

I think this function will help you:

echo get_hashtags($string);



function get_hashtags($string, $str = 1) {
    preg_match_all('/#(\w+)/',$string,$matches);
    $i = 0;
    if ($str) {
        foreach ($matches[1] as $match) {
            $count = count($matches[1]);
            $keywords .= "$match";
            $i++;
            if ($count > $i) $keywords .= ", ";
        }
    } else {
        foreach ($matches[1] as $match) {
            $keyword[] = $match;
        }
        $keywords = $keyword;
    }
    return $keywords;
}

Solution 4

Extract multiple hashtag to array

$body = 'My #name is #Eminem, I am rap #god, #Yoyoya check it #out';
$hashtag_set = [];
$array = explode('#', $body);

foreach ($array as $key => $row) {
    $hashtag = [];
    if (!empty($row)) {
        $hashtag =  explode(' ', $row);
        $hashtag_set[] = '#' . $hashtag[0];
    }
}
print_r($hashtag_set);

Solution 5

You can use preg_match_all() PHP function

preg_match_all('/(?<!\w)#\w+/', $description, $allMatches);

will give you only hastag array

preg_match_all('/#(\w+)/', $description, $allMatches);

will give you hastag and without hastag array

print_r($allMatches)
Share:
13,403
Zeid Selimovic
Author by

Zeid Selimovic

Updated on July 11, 2022

Comments

  • Zeid Selimovic
    Zeid Selimovic almost 2 years

    I need some help with twitter hashtag, I need to extract a certain hashtag as string variable in PHP. Until now I have this

    $hash = preg_replace ("/#(\\w+)/", "<a href=\"http://twitter.com/search?q=$1\">#$1</a>", $tweet_text);
    

    but this just transforms hashtag_string into link

  • Zeid Selimovic
    Zeid Selimovic almost 12 years
    Thanks a lot, this was helpful.. keep up good work guys. peace
  • Jeremy Luisetti
    Jeremy Luisetti over 8 years
    Thanks, that's an elegant way to do it.
  • Positivity
    Positivity about 5 years
    Two flaws, first does not accept underscores, and second, ignores unicode chars
  • nickb
    nickb about 5 years
    @Webinan \w matches underscore. For unicode, add the u flag to the end of the regex.
  • Positivity
    Positivity about 5 years
    @nickb Oh, third, only extracts the first hashtag in the string :)
  • nickb
    nickb about 5 years
    @Webinan That's not the scope of the OP. This answer applies to the question that was originally asked, nothing more.
  • Uriahs Victor
    Uriahs Victor over 2 years
    @nickb It might have been better to also show an example of allowing underscore and unicode characters instead of just mentioning it in a comment