snorpey snorpey - 1 year ago 82
PHP Question

Retrieve all hashtags from a tweet in a PHP function

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:


Answer Source
$tweet = "this has a #hashtag a  #badhash-tag and a #goodhash_tag";

preg_match_all("/(#\w+)/", $tweet, $matches);

var_dump( $matches );

*Dashes are illegal chars for hashtags, underscores are allowed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download