Last active
June 14, 2017 02:12
-
-
Save eli-oat/1e83ab9e54d76659070286c8952b13e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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