get meta description , title and image from url like facebook link sharing

≡放荡痞女 提交于 2019-11-30 19:17:15

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.

I know the question is 1.5 years old. But if you are still looking for it, you can use https://urlmeta.org. Its a free API to extract URL meta.

You can check a url for http or https by

$url='stackoverflow.com';
$http_check='http://';
$https_check='http://';
if(substr($url,0,7)!=$http_check){
   $url=$http_check.$url;
}else if(substr($url,0,8)!=$https_check){
   $url=$https_check.$url;
}else{
    $url=$url
}

then you can use above answer

<?php
$tags = get_meta_tags($url);
echo "<pre>";
print_r($tags);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!