Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony's DomCrawler how can I extract meta description from html source

Tags:

php

symfony

Using Symfony's DomCrawler how can I extract meta description from html source? http://symfony.com/doc/current/components/dom_crawler.html

$crawler = new Crawler();
$crawler->addHtmlContent($html->content, 'UTF-8');

$title = $crawler->filter('title')->text();

Example MSN meta description

<meta name="description" content="The new MSN, Your customizable collection of the best in news, sports, entertainment, money, weather, travel, health, and lifestyle, combined with Outlook, Facebook, Twitter, Skype, and more."/>
like image 304
Jason Avatar asked Jan 20 '16 05:01

Jason


2 Answers

I assume you are trying to get content attribute value so try to use

$data = $crawler->filterXpath("//meta[@name='description']")->extract(array('content'));

and loop through $data.

like image 113
xamoxer1 Avatar answered Oct 23 '22 17:10

xamoxer1


$meta_description = $crawler->filter('meta[name="description"]')->eq(0)->attr('content');
like image 5
Luca Filosofi Avatar answered Oct 23 '22 19:10

Luca Filosofi