开发者

recognize hash tags in twitter statuses

开发者 https://www.devze.com 2023-03-27 17:24 出处:网络
So I am parsing twitter statuses using PHP and I want to write code that recognizes whether the person added a specific tag (#php). The code always searches for the #php tag and determines whether the

So I am parsing twitter statuses using PHP and I want to write code that recognizes whether the person added a specific tag (#php). The code always searches for the #php tag and determines whether the person added th开发者_开发知识库e tag. I am only searching for the #php tag. How can I write such a code in PHP?


With preg_match():

$has_php = preg_match('/#php\b/i', $tweet);

It's case-insensitive.


$has_hash = (boolean) strpos($status, '#php');


This solution is based on @daGrevis ones, but fixes two bugs:

$has_hash = (($has_hash = stripos($status, '#php')) !== false && $has_hash >= 0);

This will also return true if the $status begins with #php. If you use just strpos, it would return false, since it's position is 0 (== false) and it also returns true if #Php or something is found (case insensitive).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号