【问题标题】:Filtering Tweets by hash tag按哈希标签过滤推文
【发布时间】:2011-10-25 19:36:36
【问题描述】:

我需要过滤掉包含特定主题标签的推文并将其显示到我网站上的提要中

所以

  1. 显示我发布的所有推文
  2. 显示包含某些哈希标签的回复推文

我一直在网上搜索,找不到任何东西

正在使用 blogger.js 但不在乎我是否需要更改方法(php 可以)

有人看过这个或者知道我可以使用的方法吗?

【问题讨论】:

  • 我认为你只需要正则表达式...
  • 你用什么来获取这些推文?服务器(即:PHP)处理它们是否重要,或者它们是否可以直接提供给用户?

标签: php javascript html twitter


【解决方案1】:

你可能需要这样的东西。

/i 用于不区分大小写的匹配(因为大小写会有所不同)。

<?php
$tweets = array("this is a #tiger tweet", "this is a #blood tweet", this is a #horror tweet");
$pattern = array("/#tiger/i", "/#blood/i", "/#horror/i");
for($i = 0; $i < count($pattern); $i++){
   for($j = 0; $j < count($tweets); $j++){
      preg_match($pattern[i], $tweets[j], $matches, PREG_OFFSET_CAPTURE);
      print_r($matches); //check matches array and decide if you want to show tweet.
   }
}
?>

或者如果您不想使用正则表达式。

<?php
$tweets = array("this is a #tiger tweet", "this is a #blood tweet", this is a #horror tweet");
$pattern = array("#tiger", "#blood", "#horror");
for($i = 0; $i < count($pattern); $i++){
   for($j = 0; $j < count($tweets); $j++){
      $tweet = strtolower(tweets[j]); //lowercase both because we we want to match the tag no matter what the case.
      $pattern = strtolower(patterns[i]);
      if(strpos(tweets[j], pattern[i]) > -1){
      //show
      }
   }
}
?>

希望这会有所帮助。

问候,

【讨论】:

  • strpos() 支持混合针,因此无需对其进行迭代。另外,为了清楚起见,我会使用strpos() !== FALSE,但这只是我。
  • 感谢您的改进。 @jeff 我没有更新代码,因为我假设您可以进行 NullUserException 所说的更改。
猜你喜欢
  • 2018-05-19
  • 2019-03-09
  • 2014-03-04
  • 2011-08-06
  • 2015-09-18
  • 1970-01-01
  • 2017-05-25
  • 2012-11-28
相关资源
最近更新 更多