PHP:如何使用Twitter的API数据的URL,提到和hastags转换成鸣叫链接? [英] PHP: How to use the Twitter API's data to convert URLs, mentions, and hastags in tweets to links?

查看:182
本文介绍了PHP:如何使用Twitter的API数据的URL,提到和hastags转换成鸣叫链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的难倒在Twitter上如何预期其API的用户将其发送到正确链接HTML明文转换鸣叫

I'm really stumped on how Twitter expects users of its API to convert the plaintext tweets it sends to properly linked HTML.

这里的交易:Twitter的API JSON发送此信息集回来时,你要求的详细数据鸣叫:

Here's the deal: Twitter's JSON API sends this set of information back when you request the detailed data for a tweet:

{
    "created_at":"Wed Jul 18 01:03:31 +0000 2012",
    "id":225395341250412544,
    "id_str":"225395341250412544",
    "text":"This is a test tweet. #boring @nbc http://t.co/LUfDreY6 #skronk @crux http://t.co/VpuMlaDs @twitter",
    "source":"web",
    "truncated":false,
    "in_reply_to_status_id":null,
    "in_reply_to_status_id_str":null,
    "in_reply_to_user_id":null,
    "in_reply_to_user_id_str":null,
    "in_reply_to_screen_name":null,
    "user": <REDACTED>,
    "geo":null,
    "coordinates":null,
    "place":null,
    "contributors":null,
    "retweet_count":0,
    "entities":{
        "hashtags":[
            {
                "text":"boring",
                "indices":[22,29]
            },
            {
                "text":"skronk",
                "indices":[56,63]
            }
        ],
        "urls":[
            {
                "url":"http://t.co/LUfDreY6",
                "expanded_url":"http://www.twitter.com",
                "display_url":"twitter.com",
                "indices":[35,55]
            },
            {
                "url":"http://t.co/VpuMlaDs",
                "expanded_url":"http://www.example.com",
                "display_url":"example.com",
                "indices":[70,90]
            }
        ],
        "user_mentions":[
            {
                "screen_name":"nbc",
                "name":"NBC",
                "id":26585095,
                "id_str":"26585095",
                "indices":[30,34]
            },
            {
                "screen_name":"crux",
                "name":"Z. D. Smith",
                "id":407213,
                "id_str":"407213",
                "indices":[64,69]
            },
            {
                "screen_name":"twitter",
                "name":"Twitter",
                "id":783214,
                "id_str":"783214",
                "indices":[91,99]
            }
        ]
    },
    "favorited":false,
    "retweeted":false,
    "possibly_sensitive":false
}

有趣的部分,对于这个问题,是文本元素,并在主题标签,<$项C $ C> user_mentions 和网​​址阵列。微博告诉我们,其中在文本元素hastags,提到和URL出现在指数阵列...所以这里的问题的症结所在:

The interesting parts, for this question, are the text element and the entries in the hashtags, user_mentions, and urls arrays. Twitter is telling us where in the text element the hastags, mentions, and urls appear with the indices arrays... so here's the crux of the question:

你怎么使用的那些指数数组?

您不能仅仅通过循环在每个链接元素的东西,如 <$ C使用它们直线上升$ C> substr_replace ,因为在文本替换的第一个链接元素将全部无效后续链接元素的索引值。你也不能使用 substr_replace 的阵列功能,因为只有当你给它一个字符串数组的第一个参数,而不是一个字符串的工作(我测试过这一点。结果是......怪)。

You can't just use them straight up by looping over each link element with something like substr_replace, since replacing the first link element in the text will invalidate all the index values for subsequent link elements. You also can't use substr_replace's array functionality, since it only works when you give it an array of strings for the first arg, rather than a single string (I've tested this. The results are... strange).

有一些功能,可以同时在一个字符串用不同的替换字符串替换多个索引分隔子?

Is there some function that can simultaneously replace multiple index-delimited substrings in a single string with different replacement strings?

推荐答案

所有你所要做的使用Twitter提供的指数直线上升一个简单的替换是收集你想,然后向后梳理它们的替代品。你也许可以找到一个更聪明的方式来建立$实体,我想反正他们可选的,所以我吻尽可能的去了。

All you have to do to use the indices twitter provides straight up with a simple replace is collect the replacements you want to make and then sort them backwards. You can probably find a more clever way to build $entities, I wanted them optional anyway, so I KISS as far as that went.

无论哪种方式,在这里我的观点只是表明你不需要爆炸的字符串,字符数和诸如此类的东西。不管你怎么做,所有你需要的就是开始于年底携手到字符串的开头,并且索引Twitter的仍然是有效的。

Either way, my point here was just to show that you don't need to explode the string and character count and whatnot. Regardless of how you do it, all you need to to is start at the end and work to the beginning of the string, and the index twitter has is still valid.

<?php 

function json_tweet_text_to_HTML($tweet, $links=true, $users=true, $hashtags=true)
{
    $return = $tweet->text;

    $entities = array();

    if($links && is_array($tweet->entities->urls))
    {
        foreach($tweet->entities->urls as $e)
        {
            $temp["start"] = $e->indices[0];
            $temp["end"] = $e->indices[1];
            $temp["replacement"] = "<a href='".$e->expanded_url."' target='_blank'>".$e->display_url."</a>";
            $entities[] = $temp;
        }
    }
    if($users && is_array($tweet->entities->user_mentions))
    {
        foreach($tweet->entities->user_mentions as $e)
        {
            $temp["start"] = $e->indices[0];
            $temp["end"] = $e->indices[1];
            $temp["replacement"] = "<a href='https://twitter.com/".$e->screen_name."' target='_blank'>@".$e->screen_name."</a>";
            $entities[] = $temp;
        }
    }
    if($hashtags && is_array($tweet->entities->hashtags))
    {
        foreach($tweet->entities->hashtags as $e)
        {
            $temp["start"] = $e->indices[0];
            $temp["end"] = $e->indices[1];
            $temp["replacement"] = "<a href='https://twitter.com/hashtag/".$e->text."?src=hash' target='_blank'>#".$e->text."</a>";
            $entities[] = $temp;
        }
    }

    usort($entities, function($a,$b){return($b["start"]-$a["start"]);});


    foreach($entities as $item)
    {
        $return = substr_replace($return, $item["replacement"], $item["start"], $item["end"] - $item["start"]);
    }

    return($return);
}


?>

这篇关于PHP:如何使用Twitter的API数据的URL,提到和hastags转换成鸣叫链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆