All pastes #66681 Raw Edit

Mine

public text v1 · immutable
#66681 ·published 2006-06-17 23:17 UTC
rendered paste body
function getDocumentTitle($url) {
	
	$pURL = parse_url($url);
	if (empty($pURL['host'])) {
		return $url;
	}

	if (empty($pURL['path'])) {
		$pURL['path'] = '/';
	}
	
	if (empty($pURL['query'])) {
		$document = $pURL['path'];
	} else {
		$document = $pURL['path'] . '?' . $pURL['query'];
	}
	
	$fp = fsockopen($pURL['host'], 80, $errno, $errstr, 30);
	if (!$fp) {
		return $url;
	} else {
		$out = "GET $document HTTP/1.1\r\n";	
		$out .= "Host: {$pURL['host']}\r\n";
		$out .= "Connection: Close\r\n\r\n";

		$received = '';
		fwrite($fp, $out);
		while (!feof($fp)) {
			$received .= fgets($fp, 128);
		}
		fclose($fp);
	}
	
	if (!preg_match('~<title>(.*)</title>~isu', $received, $matches)) {
		return $url;
	}
	
	return $matches[1];
}