PHP :如何计算 Twitter 推文中的 URL、主题标签和 user_mentions 的数量
PHP : How to count the number of URLs, hashtags and user_mentions in a Twitter tweet
我想计算推文中发布的 URL 和#HashTag 的数量。我正在使用 PHP 扫描 Twitter 提要。
是否可以获取 Twitter 推文中发布的 URL 和#hashtags 的数量?
我如何得到这个?
编辑:
我试过这个代码
preg_match_all('/#\S*\w/i', $tweet, $matches);
var_dump( $matches );
输出:
array(1) { //count1
[0]=>
array(1) {
[0]=>
string(12) "#GoodMorning"
}
}
array(1) { //count2
[0]=>
array(1) {
[0]=>
string(11) "#HelloWorld"
}
}
array(1) { //count3
[0]=>
array(0) {
}
}
array(1) { //count4
[0]=>
array(0) {
}
}
预期输出:
主题标签数 = 2
网址数量 = 3(如果有)
感谢您的帮助
基于 Retrieve all hashtags from a tweet in a PHP function 中的答案,只需添加输出计数。
$tweet = "this has a #hashtag a #badhash-tag and a #goodhash_tag";
preg_match_all("/(#\w+)/", $tweet, $matches);
count($matches[0]);
我想计算推文中发布的 URL 和#HashTag 的数量。我正在使用 PHP 扫描 Twitter 提要。 是否可以获取 Twitter 推文中发布的 URL 和#hashtags 的数量?
我如何得到这个?
编辑:
我试过这个代码
preg_match_all('/#\S*\w/i', $tweet, $matches);
var_dump( $matches );
输出:
array(1) { //count1
[0]=>
array(1) {
[0]=>
string(12) "#GoodMorning"
}
}
array(1) { //count2
[0]=>
array(1) {
[0]=>
string(11) "#HelloWorld"
}
}
array(1) { //count3
[0]=>
array(0) {
}
}
array(1) { //count4
[0]=>
array(0) {
}
}
预期输出:
主题标签数 = 2 网址数量 = 3(如果有)
感谢您的帮助
基于 Retrieve all hashtags from a tweet in a PHP function 中的答案,只需添加输出计数。
$tweet = "this has a #hashtag a #badhash-tag and a #goodhash_tag";
preg_match_all("/(#\w+)/", $tweet, $matches);
count($matches[0]);