duanhuan7750 2014-04-18 20:27
浏览 38
已采纳

从Facebook链接共享的URL获取元描述,标题和图像

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

this does not shows result for all the url's , example http://google.com

展开全部

  • 写回答

3条回答 默认 最新

  • du970294 2014-04-18 20:36
    关注

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

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

    Illustration :

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部