我希望能够从给定的URL抓取网页的标题,很像Facebook和最近的Google plus有一个链接预览器,但更简化。
关于如何做到这一点有什么提示/技巧吗?
如果你在php上写,这应该可以完成你的工作
function getdoctitle($link) {
$lines = @file($link);
$str=implode(”'n”,$lines);
$str=preg_match('/<title>([^>]*)<'/title>/si', $str, $matches );
if (strpos(” $str”,”<title>”) and strpos(” $str”,”</title>”)) {
$a1=explode(”<title>”,$str);
$str2=$a1[1];
$a2=explode(”</title>”,$str2);
$str3=$a2[0];
return $str3;
} else {
return “”;
}
}
最好使用jQuery
//get_new_title.php:
<?php
if ($_GET['link'] == "#new_stuff")
{
echo "My site name - New STUFF!!!";
}
if ($_GET['link'] == "#other stuff")
{
echo "My site name - Some other stuff";
}
//javascript$(function(){
$("a").bind('click', function(){
$.ajax({
url: 'get_new_title.php?link='+$(this).attr('href'),
success: function(title){
document.title = title;
}
});
//do more stuff here
}
});