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).
精彩评论