开发者

facebook share links script

开发者 https://www.devze.com 2023-03-13 19:42 出处:网络
i have a page like this <html> <head> <title>mytitle</title> </head> <body>

i have a page like this

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

i need a facebook share like script ,, so when any one type a website url , this script get website title ,, images as thum开发者_Python百科bnails and some data from body tag .. any idea?


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";


<?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>

I hope this script works well.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号