Sunday 15 June 2014

php - Parse attribute data of certain HTML element -



php - Parse attribute data of certain HTML element -

let's have html page contains this:

<meta property="image" content="http://example.com/example.jpg" />

how can utilize php reach "meta property" , image url "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 $meta){ if($meta->getattribute('property') == 'image') { $meta_image = $meta->getattribute('content'); break; } } echo $meta_image;

do not utilize regex parse html. @kiwi1 pointed out can utilize php built-in function get_meta_tags(). altho prefer utilize dom parse html simple stuff.

php html

No comments:

Post a Comment