来自像Facebook链接共享网址获得meta描述,标题和图像 [英] get meta description , title and image from url like facebook link sharing

查看:295
本文介绍了来自像Facebook链接共享网址获得meta描述,标题和图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

my code is

       function getTitle($Url){
            $str = file_get_contents($Url);
            if(strlen($str)>0){
                preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
                return $title[1];
            }
            else
            {
                return false;
            }
        }
        function getMetas($Url){
            $str = file_get_contents($Url);
            if(strlen($str)>0){
             //   preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
               preg_match("/<meta name=\"description\" content=\"(.*?)\"/",$str,$title);
              //  preg_match( '<meta name="description".*content="([^"]+)">siU', $str, $title);
                return $title[1];
            }
             else
            {
                return false;
            }
        }

        //Example:
        $url=$_POST['url'];
        echo getTitle($url);
        echo "<br><br>";
        echo getMetas($url);

这不结果显示的是所有URL的,例如 http://google.com

推荐答案

为什么要使用解析&LT定期EX pression;元&GT; 标签?

Why are you using regular expression for parsing the <meta> tags ?

PHP有解析元的内置功能信息,因此被称为的 get_meta_tags()

PHP has an in-built function for parsing the meta information , it is called the get_meta_tags()

<?php
$tags = get_meta_tags('http://www.stackoverflow.com/');
echo "<pre>";
print_r($tags);

输出:

OUTPUT:

Array
(
    [twitter:card] => summary
    [twitter:domain] => stackoverflow.com
    [og:type] => website
    [og:image] => http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon@2.png?v=fde65a5a78c6
    [og:title] => Stack Overflow
    [og:description] => Q&A for professional and enthusiast programmers
    [og:url] => http://stackoverflow.com/
)

正如你可以看到的标题图片说明的被解析,你真正想要的。

As you can see the title , image and description are being parsed which you really want.

这篇关于来自像Facebook链接共享网址获得meta描述,标题和图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆