Let's say I have an HTML page which contains this:
<meta property="image" content="http://example.com/example.jpg" />
How can I use PHP to reach that "meta property" and get the image URL from "content"?
Thanks!
Let's say I have an HTML page which contains this:
<meta property="image" content="http://example.com/example.jpg" />
How can I use PHP to reach that "meta property" and get the image URL from "content"?
Thanks!
Working live: http://codepad.org/ggUwL06F
$doc = new DOMDocument();
$doc->loadHTML('<meta property="image" content="http://example.com/example.jpg" />');
$metas = $doc->getElementsByTagName('meta');
foreach($metas as $meta){
if($meta->getAttribute('property') == 'image') {
$meta_image = $meta->getAttribute('content');
break;
}
}
echo $meta_image;
Do not use regex to parse HTML. Also as @Kiwi1 pointed out you can use the PHP built-in function get_meta_tags(). Altho I prefer to use DOM to parse HTML even for simple stuff.