Facebook就像链接解析器

时间:2011-09-05 10:39:59

标签: php facebook

是否已经完成了可以解析Facebook,Google +或Digg等链接的php类?要从页面获取标题,一些文本和图像? :)

由于

3 个答案:

答案 0 :(得分:1)

以下是一些代码pinched from sitepoint.com。我已经使用了几次,它看起来效果很好......

<?php

  define( 'LINK_LIMIT', 30 );
  define( 'LINK_FORMAT', '<a href="%s" rel="ext">%s</a>' );
  function parse_links  ( $m ){
    $href = $name = html_entity_decode($m[0]);
    if ( strpos( $href, '://' ) === false ) {
      $href = 'http://' . $href;
    }
    if( strlen($name) > LINK_LIMIT ) {
      $k = ( LINK_LIMIT - 3 ) >> 1;
      $name = substr( $name, 0, $k ) . '...' . substr( $name, -$k );
    }
    return sprintf( LINK_FORMAT, htmlentities($href), htmlentities($name) );
  }
  $s = 'Here is a text - www.ellehauge.net - it has some links with e.g. comma, www.one.com,in it. Some links look like this: http://mail.google.com - mostly they end with aspace or carriage return www.unis.no<br /> - but they may also end with a period: http://ellehauge.net. You may even putthe links in brackets (www.skred-svalbard.no) (http://one.com).From time to time, links use a secure protocol like https://gmail.com |This.one.is.a.trick. Sub-domaines: http://test.ellehauge.net |www.test.ellehauge.net | Files: www.unis.no/photo.jpg |Vars: www.unis.no?one=1&amp;~two=2 | No.: www.unis2_check.no/doc_under_score.php |www3.one.com | another tricky one:http://ellehauge.net/cv_by_id.php?id%5B%5D=105&amp;id%5B%5D=6&amp;id%5B%5D=100';
  $reg = '~((?:https?://|www\d*\.)\S+[-\w+&@#/%=\~|])~';
  print preg_replace_callback( $reg, 'parse_links', $s );

?>

答案 1 :(得分:1)

答案 2 :(得分:0)

的index.php

<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<style>body
{
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
}
#contentbox
{
width:458px; height:50px;
border:solid 2px #dedede;
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
margin-bottom:6px;
}
.img
{
float:left; width:150px; margin-right:10px;
text-align:center;
}
#linkbox
{
border:solid 2px #dedede; min-height:50px; padding:15px;
display:none;
}</style>
<script type="text/javascript">
$(document).ready(function()
{
$("#contentbox").keyup(function()
{
var content=$(this).val();
var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
// Filtering URL from the content using regular expressions
var url= content.match(urlRegex);

if(url.length>0)
{
$("#linkbox").slideDown('show');
$("#linkbox").html("<img src='link_loader.gif'>");
// Getting cross domain data 
$.get("urlget.php?url="+url,function(response)
{
// Loading <title></title>data
var title=(/<title>(.*?)<\/title>/m).exec(response)[1];
// Loading first .png image src='' data 
var logo=(/src='(.*?).png'/m).exec(response)[1];
$("#linkbox").html("<img src='"+logo+".png' class='img'/><div><b>"+title+"</b><br/>"+url)
});

}
return false;
});
});
//HTML Code
<textarea id="contentbox"></textarea>
<div id="linkbox"></div>
</script>

urlget.php

<?php
if($_GET['url'])
{
$url=$_GET['url'];
echo file_get_contents($url);
}
?>

source