0

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!

3
  • What did you tried so far? If it is a string, then you can use regular expression. If this is a rendered page, you need to use javascript and regexp to get that string. Commented Nov 3, 2014 at 13:23
  • You can't. The html is client side and PHP server side. What do you actually want to do? When do you want to get the value in php? When the page refreshes or navigates? Commented Nov 3, 2014 at 13:24
  • 1
    Do you actually grab/scrape content from some site, and want to get image url? Please provide more data, whole context... Commented Nov 3, 2014 at 13:29

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

PHP has a function that extracts all meta tag content attributes from a URL. Look for it at the PHP docs.

$tags = get_meta_tags('http://www.url.com/');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.