我正在做一个书签系统,并寻找使用 PHP检索页面标题的最快(最简单的)方法. 这将是很高兴有一些像$title = page_title($url) ?php function page_title($url) { $fp = file_get_contents($url); if (!$fp) return null
这将是很高兴有一些像$title = page_title($url)
<?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;
}
?>
在以下输入中提供“旋转”
print page_title("http://www.google.com/");
输出:Google
希望一般足够你的使用.如果您需要更强大的功能,那么投入一些时间来研究HTML解析器可能不会受到伤害.
编辑:添加了一点错误检查.种类冲第一个版本,对不起.
