duanhuan7750 2014-04-19 04: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-19 04: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条)

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换