Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP: How to use the Twitter API's data to convert URLs, mentions, and hastags in tweets to links?
    text
    copied!<p>I'm really stumped on how Twitter expects users of its API to convert the plaintext tweets it sends to properly linked HTML.</p> <p>Here's the deal: Twitter's JSON API sends this set of information back when you request the detailed data for a tweet:</p> <pre><code>{ "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": &lt;REDACTED&gt;, "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 } </code></pre> <p>The interesting parts, for this question, are the <code>text</code> element and the entries in the <code>hashtags</code>, <code>user_mentions</code>, and <code>urls</code> arrays. Twitter is telling us where in the <code>text</code> element the hastags, mentions, and urls appear with the <code>indices</code> arrays... so here's the crux of the question:</p> <p>How do you <em>use</em> those <code>indices</code> arrays?</p> <p>You can't just use them straight up by looping over each link element with something like <a href="http://php.net/manual/en/function.substr-replace.php"><code>substr_replace</code></a>, since replacing the first link element in the <code>text</code> will invalidate all the index values for subsequent link elements. You also can't use <a href="http://php.net/manual/en/function.substr-replace.php"><code>substr_replace</code></a>'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).</p> <p>Is there some function that can simultaneously replace multiple index-delimited substrings in a single string with different replacement strings?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload