Extracting Twitter hashtag from string in PHP

2020-03-07 05:54发布

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

7条回答
相关推荐>>
2楼-- · 2020-03-07 06:44

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

查看更多
登录 后发表回答