<?php
function page_title($url) {
$fp = file_get_contents($url);
if (!$fp)
return null;
$res = preg_match("/<title>(.*)</title>/siU", $fp, $title_matches);
if (!$res)
return null;
// Clean up title: remove EOL's and excessive whitespace.
$title = preg_replace('/s+/', ' ', $title_matches[1]);
$title = trim($title);
return $title;
}
?>
Gave 'er a whirl on the following input:
print page_title("http://www.google.com/");
Outputted: Google
Hopefully general enough for your usage. If you need something more powerful, it might not hurt to invest a bit of time into researching HTML parsers.
EDIT: Added a bit of error checking. Kind of rushed the first version out, sorry.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…