Get title of website via link

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.

<?php

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

?>

OUTPUT

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

Leave a Comment