TwitterがAPIのユーザーに、送信するプレーンテキストのツイートを適切にリンクされたHTMLに変換することをどのように期待しているかに本当に困惑しています。
取引は次のとおりです。TwitterのJSONAPIは、ツイートの詳細データをリクエストすると、この一連の情報を送り返します。
{
"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
}
この質問の興味深い部分は、、、、および配列の要素とエントリですtext
。Twitterは、要素のどこにハスタッグ、メンション、URLが配列とともに表示されるかを教えてくれます...それで、質問の核心は次のとおりです。hashtags
user_mentions
urls
text
indices
それらのアレイをどのように使用しますか?indices
substr_replace
の最初のリンク要素を置き換えると、text
後続のリンク要素のすべてのインデックス値が無効になるため、各リンク要素をのようなものでループして、それらをまっすぐに使用することはできません。また、の配列機能を使用することはできませんsubstr_replace
。これは、単一の文字列ではなく、最初のargの文字列の配列を指定した場合にのみ機能するためです(これをテストしました。結果は...奇妙です)。
1つの文字列内の複数のインデックス区切りのサブ文字列を異なる置換文字列で同時に置換できる関数はありますか?