0

Right, based on this question Tweet clickable links with twitteroauth?,

How do I actually parse the entire string and replace the t.co link portion with a

<a href=""></a>

portion?

For example i tweet this -

Hey check out this link www.google.com

and in my website currently it shows

Hey check out this link http://t.co/generatedlink

So how do i parse it and make it into this

Hey check out this link <a href="http://t.co/generatedlink">http://t.co/generatedlink</a>

which would display like this in my website:

Hey check out this link http://t.co/generatedlink

How am I able to detect that a certain portion of the tweet text has a link inside it? Or am I going about this wrong?

Community
  • 1
  • 1
Kyle Yeo
  • 2,270
  • 5
  • 31
  • 53

3 Answers3

1

You need to understand Twitter Entities.

When you request the tweet, make sure you use include_entities=true

For example:

https://api.twitter.com/1/statuses/show.json?id=220197158798897155&include_entities=true

In the response, you will see an element called "entities" inside that, you will see "urls".

That will contain all the URLs and their position (indices) within the tweet.

"text": "Twitter for Mac is now easier and faster, and you can open multiple windows at once http://t.co/0JG5Mcq",

"entities": {

  "urls": [

    {

      "url": "http://t.co/0JG5Mcq",

      "display_url": "blog.twitter.com/2011/05/twitte…",

      "expanded_url": "http://blog.twitter.com/2011/05/twitter-for-mac-update.html",

      "indices": [

        84,

        103

      ]

    }

  ],

}
Terence Eden
  • 14,034
  • 3
  • 48
  • 89
0
function urlify(text) {
    var urlRegex = /(https?:\/\/[^\s]+)/g;
    return text.replace(urlRegex, function(url) {
        return '<a href="' + url + '">' + url + '</a>';
    })}

var text = "Hey check out this link http://t.co/generatedlink";
var a = urlify(text);
alert(a);

The above solution is perfect for your needs. I have a Fiddle link too Check it out http://jsfiddle.net/UhzCx/

I got this answer here Detect URLs in text with JavaScript

Community
  • 1
  • 1
Shiv Kumar Ganesh
  • 3,799
  • 10
  • 46
  • 85
0

If you can use javascript, here is an example http://jsfiddle.net/3VF96/13/

Function

function generateURL(text){
   var str=text;
    var n=str.indexOf("http");
    var strv=str.substring(0,n);
    var link=str.substring(n,str.length);
    strv=strv+" <a href='"+link+"'>"+link+"</a>";
      $("#Link").append(strv);
}
​
Ashwin Singh
  • 7,197
  • 4
  • 36
  • 55