facebook分享链接脚本

时间:2011-06-17 06:30:23

标签: php javascript jquery facebook

我有一个这样的页面

<html>
<head>
<title>mytitle</title>
</head>
<body>
 my data 
 some pics...
</body>
 data and some pics 
</html>

我需要像脚本一样的facebook分享,所以当任何一个类型的网站网址,这个脚本获取网站标题,图像作为缩略图和身体标记的一些数据..任何想法?

2 个答案:

答案 0 :(得分:0)

function getMetaTitle($content){
    $pattern = "|<[\s]*title[\s]*>([^<]+)<[\s]*/[\s]*title[\s]*>|Ui";
    if(preg_match($pattern, $content, $match))
    return $match[1];
    else
    return false;
}
function getMetaDescription($content) {
    $metaDescription = false;
    $metaDescriptionPatterns = array("/]*>/Ui", "/]*>/Ui");
    foreach ($metaDescriptionPatterns as $pattern) {
    if (preg_match($pattern, $content, $match))
    $metaDescription = $match[1];
    break;
    }
    return $metaDescription;
} 


   function getExcerpt($content) {
    $text = html_entity_decode($content);
    $excerpt = array();
    //match all tags
    preg_match_all("|<[^>]+>(.*)]+>|", $text, $p, PREG_PATTERN_ORDER);
    for ($x = 0; $x < sizeof($p[0]); $x++) {
    if (preg_match('< p >i', $p[0][$x])) {
        $strip = strip_tags($p[0][$x]);
        if (preg_match("/\./", $strip))
        $excerpt[] = $strip;
    }
    if (isset($excerpt[0])){
        preg_match("/([^.]+.)/", $strip,$matches);
        return $matches[1];
        }
    }
    return false;
 }

url = 'http://www.example.com/';
$content = file_get_contents($url);
$title = getMetaTitle($content);
$description = getMetaDescription($content);
$excerpt = getExcerpt($content);
print "title: $title ";
print "< br />";
print "description: $description ";
print "< br />";
print "excerpt: $excerpt";

答案 1 :(得分:0)

<?php
$url = $_REQUEST['url'];
$url = checkValues($url);

function checkValues($value)
{
$value = trim($value);
if (get_magic_quotes_gpc()) 
{
    $value = stripslashes($value);
}
$value = strtr($value, array_flip(get_html_translation_table(HTML_ENTITIES)));
$value = strip_tags($value);
$value = htmlspecialchars($value);
return $value;
}   

function fetch_record($path)
{
$file = fopen($path, "r"); 
if (!$file)
{
    exit("Problem occured");
} 
$data = '';
while (!feof($file))
{
    $data .= fgets($file, 1024);
}
return $data;
}

$string = fetch_record($url);
/// fecth title
$title_regex = "/<title>(.+)<\/title>/i";
preg_match_all($title_regex, $string, $title, PREG_PATTERN_ORDER);
$url_title = $title[1];

/// fecth decription
$tags = get_meta_tags($url);

// fetch images
$image_regex = '/<img[^>]*'.'src=[\"|\'](.*)[\"|\']/Ui';
preg_match_all($image_regex, $string, $img, PREG_PATTERN_ORDER);
$images_array = $img[1];
?>
<div class="images">
<?php
$k=1;
for ($i=0;$i<=sizeof($images_array);$i++)
{
    if(@$images_array[$i])
    {
        if(@getimagesize(@$images_array[$i]))
        {
            list($width, $height, $type, $attr) = getimagesize(@$images_array[$i]);
            if($width >= 50 && $height >= 50 ){

            echo "<img src='".@$images_array[$i]."' width='100' id='".$k."' >";

            $k++;

            }
        }
    }
}
?>
<!--<img src="ajax.jpg"  alt="" />-->
<input type="hidden" name="total_images" id="total_images" value="<?php echo --$k?>" />
</div>
<div class="info">

    <label class="title">
        <?php  echo @$url_title[0]; ?>
    </label>
    <br clear="all" />
    <label class="url">
        <?php  echo substr($url ,0,35); ?>
    </label>
    <br clear="all" /><br clear="all" />
    <label class="desc">
        <?php  echo @$tags['description']; ?>
    </label>
    <br clear="all" /><br clear="all" />

    <label style="float:left"><img src="prev.png" id="prev" alt="" /><img src="next.png" id="next" alt="" /></label>

    <label class="totalimg">
        Total <?php echo $k?> images
    </label>
    <br clear="all" />

</div>

我希望这个脚本运行良好。

来源:http://www.99points.info/2010/07/facebook-like-extracting-url-data-with-jquery-ajax-php/