【问题标题】:Extracting Twitter hashtag from string in PHP从 PHP 中的字符串中提取 Twitter 主题标签
【发布时间】:2012-06-15 22:00:50
【问题描述】:

我需要一些关于 twitter 主题标签的帮助,我需要在 PHP 中提取某个主题标签作为字符串变量。 到现在为止我有这个

$hash = preg_replace ("/#(\\w+)/", "<a href=\"http://twitter.com/search?q=$1\">#$1</a>", $tweet_text);

但这只是将 hashtag_string 转换为链接

【问题讨论】:

  • 您是要获取主题标签还是删除它?或两者?你所做的只是匹配一个主题标签模式,然后替换为完全相同的字符串。
  • 您实际上只是用相同的东西替换主题标签......
  • 一方面,preg_replace 就像名字所暗示的那样:它取代了一些东西 ...preg_match 在语义上更适合你想要完成的事情。此外,如果您发布您要从中提取的源字符串的基本示例以及您想要从该源字符串中提取的确切内容的示例,则会更容易提供帮助。
  • 关于 SO 的唯一完全有效的答案:stackoverflow.com/a/16609221

标签: php twitter hashtag


【解决方案1】:

使用preg_match() 识别哈希并将其捕获到变量中,如下所示:

$string = 'Tweet #hashtag';
preg_match("/#(\\w+)/", $string, $matches);
$hash = $matches[1];
var_dump( $hash); // Outputs 'hashtag'

Demo

【讨论】:

  • 非常感谢,这很有帮助.. 继续努力,伙计们。和平
  • 两个缺陷,一是不接受下划线,二是忽略unicode chars
  • @Webinan \w 匹配下划线。对于 unicode,将 u 标志添加到正则表达式的末尾。
  • @nickb 哦,第三,只提取字符串中的第一个标签:)
  • @Webinan 这不是 OP 的范围。此答案适用于最初提出的问题,仅此而已。
【解决方案2】:

您可以使用preg_match function提取字符串中的值

preg_match("/#(\w+)/", $tweet_text, $matches);
$hash = $matches[1];

preg_match 将匹配结果存储在一个数组中。您应该查看文档以了解如何使用它。

【讨论】:

    【解决方案3】:

    这是一种非正则表达式的方法:

    <?php
    
    $tweet = "Foo bar #hashTag hello world";
    
    $hashPos = strpos($tweet,'#');
    $hashTag = '';
    
    while ($tweet[$hashPos] !== ' ') {
     $hashTag .= $tweet[$hashPos++];
    }
    
    echo $hashTag;
    

    Demo

    注意:这只会拾取推文中的第一个主题标签。

    【讨论】:

      【解决方案4】:

      我认为这个功能会对你有所帮助:

      echo get_hashtags($string);
      
      
      
      function get_hashtags($string, $str = 1) {
          preg_match_all('/#(\w+)/',$string,$matches);
          $i = 0;
          if ($str) {
              foreach ($matches[1] as $match) {
                  $count = count($matches[1]);
                  $keywords .= "$match";
                  $i++;
                  if ($count > $i) $keywords .= ", ";
              }
          } else {
              foreach ($matches[1] as $match) {
                  $keyword[] = $match;
              }
              $keywords = $keyword;
          }
          return $keywords;
      }
      

      【讨论】:

        【解决方案5】:

        据我了解,您是这么说的 在 text/pargraph/post 中,您想显示带有井号 (#) 的标签,如下所示:- #tag 在 url 中你想删除 # 符号,因为# 之后的字符串没有按请求发送到服务器,所以我编辑了你的代码并尝试了这个:-

        $string="www.funnenjoy.com is best #SocialNetworking #website";    
        $text=preg_replace('/#(\\w+)/','<a href=/hash/$1>$0</a>',$string);
        echo $text; // output will be www.funnenjoy.com is best <a href=search/SocialNetworking>#SocialNetworking</a> <a href=/search/website>#website</a>
        

        【讨论】:

        • 谢谢,这是一种优雅的方式。
        【解决方案6】:

        提取多个标签到数组

        $body = 'My #name is #Eminem, I am rap #god, #Yoyoya check it #out';
        $hashtag_set = [];
        $array = explode('#', $body);
        
        foreach ($array as $key => $row) {
            $hashtag = [];
            if (!empty($row)) {
                $hashtag =  explode(' ', $row);
                $hashtag_set[] = '#' . $hashtag[0];
            }
        }
        print_r($hashtag_set);
        

        【讨论】:

          【解决方案7】:

          你可以使用preg_match_all()PHP函数

          preg_match_all('/(?<!\w)#\w+/', $description, $allMatches);
          

          只会给你hastag数组

          preg_match_all('/#(\w+)/', $description, $allMatches);
          

          会给你hastag和没有hastag数组

          print_r($allMatches)
          

          【讨论】:

            猜你喜欢
            • 2021-07-30
            • 2011-01-05
            • 2023-01-16
            • 2018-10-07
            • 2021-06-20
            • 2021-12-25
            • 2017-08-09
            • 1970-01-01
            • 2014-07-17
            相关资源
            最近更新 更多