Build a simple link previewer / linter
I w开发者_JAVA技巧ant to be able to grab the Title of a web page from a given URL, much like how Facebook and recently Google plus have a link previewer, but more simplified.
Any hints / tips on how to do this?
If you are writing on php, this should do your job
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 “”;
}
}
It will be better to do it with 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
}
});
精彩评论