【问题标题】:PHP Twitter replace link and hashtag with real linkPHP Twitter 用真实链接替换链接和标签
【发布时间】:2011-04-17 11:59:12
【问题描述】:

我正在循环来自 Twitter API 的 JSON 响应。每个 API 响应都会给我一条类似于以下内容的推文:

大家好,我的名字是@john,我喜欢#soccer,请访问我

我正在尝试替换@john,并插入<a href=http://twitter.com/john>@john</a>,但@john 之后的逗号(,)是问题所在。

如何替换标签前后的点、逗号等?

【问题讨论】:

    标签: php api twitter


    【解决方案1】:
    $str = preg_replace("/@(\w+)/i", "<a href=\"http://twitter.com/$1\">$0</a>", $str);
    

    【讨论】:

    • 抱歉,没有通知我有人已经发布了答案。
    【解决方案2】:
    preg_replace("/@(\w+)/", "<a href=http://twitter.com/$1>@$1</a>", $string)"
    

    【讨论】:

      【解决方案3】:

      要做哈希标签这样做

      $item_content = preg_replace("/#([a-z_0-9]+)/i", "<a href=\"http://twitter.com/search/$1\">$0</a>", $item_content);
      

      【讨论】:

        【解决方案4】:

        这是一个函数,它使用来自 Twitter API 的推文中的“实体”数据将主题标签、用户提及和 URL 转换为链接。

        <?php
        
        function tweet_html_text(array $tweet) {
            $text = $tweet['text'];
        
            // hastags
            $linkified = array();
            foreach ($tweet['entities']['hashtags'] as $hashtag) {
                $hash = $hashtag['text'];
        
                if (in_array($hash, $linkified)) {
                    continue; // do not process same hash twice or more
                }
                $linkified[] = $hash;
        
                // replace single words only, so looking for #Google we wont linkify >#Google<Reader
                $text = preg_replace('/#\b' . $hash . '\b/', sprintf('<a href="https://twitter.com/search?q=%%23%2$s&src=hash">#%1$s</a>', $hash, urlencode($hash)), $text);
            }
        
            // user_mentions
            $linkified = array();
            foreach ($tweet['entities']['user_mentions'] as $userMention) {
                $name = $userMention['name'];
                $screenName = $userMention['screen_name'];
        
                if (in_array($screenName, $linkified)) {
                    continue; // do not process same user mention twice or more
                }
                $linkified[] = $screenName;
        
                // replace single words only, so looking for @John we wont linkify >@John<Snow
                $text = preg_replace('/@\b' . $screenName . '\b/', sprintf('<a href="https://www.twitter.com/%1$s" title="%2$s">@%1$s</a>', $screenName, $name), $text);
            }
        
            // urls
            $linkified = array();
            foreach ($tweet['entities']['urls'] as $url) {
                $url = $url['url'];
        
                if (in_array($url, $linkified)) {
                    continue; // do not process same url twice or more
                }
                $linkified[] = $url;
        
                $text = str_replace($url, sprintf('<a href="%1$s">%1$s</a>', $url), $text);
            }
        
            return $text;
        }
        

        【讨论】:

        猜你喜欢
        • 2018-04-18
        • 1970-01-01
        • 1970-01-01
        • 2011-07-28
        • 2011-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多