-2

Recently I saw that when I put the link in my profile it replaced it with the exact title of the question. So I want to know, how do they do it?

They replaced a url with the relevant link with a title as well

I am guessing that they do this by sending the link via ajax and then make a link header by php implode ,explode. Is this correct way/same way that Stack Overflow does this?

Lucas
  • 16,930
  • 31
  • 110
  • 182
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
  • 3
    +1: this is a very valid programming question IMHO? He could have asked, I would like to convert the link I paste into my textbox as the page title. – naveen Sep 16 '12 at 05:45
  • 1 close vote for off topic ?? and 3 for not a real question ?? – NullPoiиteя Sep 16 '12 at 05:50

2 Answers2

5

Suppose I am pasting the above link in the textarea like this

http://stackoverflow.com/questions/12444652/how-to-make-a-link-system-link-stackoverw

This is what happens behind the scenes. A GET request is made to the below URL

http://api.stackexchange.com/2.0/questions/12444652?pagesize=30&key=6AU78DZ)GcdjNjAszYmTLQ((&filter=!6G7RPxWUNTleV&site=stackoverflow.com&callback=apiCallbacks[%22stackoverflow.com%22]&_=1347774461814

The response of that GET is

apiCallbacks["stackoverflow.com"]({"items":[{"question_id":12444652,"title":"How to make a link system link stackoverw"}]});

From that one could retrieve the title. Hope this helps.

naveen
  • 53,448
  • 46
  • 161
  • 251
4

I thought this was too much info to put in a comment, so I just put it as an answer. The program just simply retrieves the value of the title element on that page.

So this is the basic process:

  1. The program does a regex (or whatever) search for a url in the string.
  2. The program obtains that url.
  3. The program replaces the url with the relevant html code: ('foo', etc.)

UPDATE: This is how one would do the 3rd step with php:

$myURL = 'http://www.google.com';
preg_match('/<title>(.+)<\/title>/',file_get_contents($myURL),$matches);
$title = $matches[1];

(from here)

Community
  • 1
  • 1
Lucas
  • 16,930
  • 31
  • 110
  • 182