[SOLVED] Get title of website via link


Notice how Google News has sources on the bottom of each article excerpt.

The Guardian – ABC News – Reuters –

I’m trying to imitate that.

For example, upon submitting the URL http://www.washingtontimes.com/news/2010/dec/3/debt-panel-fails-test-vote/ I want to return The Washington Times

How is this possible with php?


My answer is expanding on @AI W’s answer of using the title of the page. Below is the code to accomplish what he said.


function get_title($url){
  $str = file_get_contents($url);
    $str = trim(preg_replace('/\s+/', ' ', $str)); // supports line breaks inside <title>
    preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); // ignore case
    return $title[1];
echo get_title("http://www.washingtontimes.com/");



Washington Times – Politics, Breaking
News, US and World News

As you can see, it is not exactly what Google is using, so this leads me to believe that they get a URL’s hostname and match it to their own list.

http://www.washingtontimes.com/ => The Washington Times

Answered By – Jose Vega

Answer Checked By – Willingham (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *