Skip to content

Instantly share code, notes, and snippets.

@eli-oat
Last active June 14, 2017 02:12
Show Gist options
  • Save eli-oat/1e83ab9e54d76659070286c8952b13e2 to your computer and use it in GitHub Desktop.
Save eli-oat/1e83ab9e54d76659070286c8952b13e2 to your computer and use it in GitHub Desktop.
<?php
function url_get_title($url)
{
if (!function_exists('curl_init'))
{
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// FAIL
if ($httpCode == 400) return $url;
// SUCCEED!
if ($httpCode == 200)
{
$str = file_get_contents($url);
if (strlen($str) > 0)
{
$str = trim(preg_replace('/\s+/', ' ', $str));
preg_match("/\<title\>(.*)\<\/title\>/i", $str, $title);
return $title[1];
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment