Reputation: 649
How can I extract a page's title
and meta description
using the PHP Simple HTML DOM Parser?
I just need the title of the page and the keywords in plain text.
Upvotes: 10
Views: 28173
Reputation: 237
I found the easy way to take description
$html = new simple_html_dom();
$html->load_file('your_url');
$title = $html->load('title')->simpletext; //<title>**Text from here**</title>
$description = $html->load("meta[name='description']", 0)->simpletext; //<meta name="description" content="**Text from here**">
If your line contains extra spaces, then try this
$title = trim($title);
$description = trim($description);
Upvotes: 0
Reputation: 31
$html->find('meta[name=keywords]',0)->attr['content'];
$html->find('meta[name=description]',0)->attr['content'];
Upvotes: 3
Reputation: 420
Taken from LeiXC's solution above, you need to use the simple html dom class:
$dom = new simple_html_dom();
$dom->load_file( 'websiteurl.com' );// put your own url in here for testing
$html = str_get_html($dom);
$descr = $html->find("meta[name=description]", 0);
$description = $descr->content;
echo $description;
I have tested this code and yes it is case sensitive (some meta tags use a capital D for description)
Here is some error checking for spelling errors:
if( is_object( $html->find("meta[name=description]", 0)) ){
echo $html->find("meta[name=description]", 0)->content;
} elseif( is_object( $html->find("meta[name=Description]", 0)) ){
echo $html->find("meta[name=Description]", 0)->content;
}
Upvotes: 4
Reputation: 1075
The correct answer is:
$html = str_get_html($html);
$descr = $html->find("meta[name=description]", 0);
$description = $descr->content;
The above code gets html into an object format, then the find method looks for a meta tag with the name description, and finally you need to return the value of the meta tag's content, not the innertext or plaintext as outlined by others.
This has been tested and used in live code. Best
Upvotes: 1
Reputation: 25
you can using php code and so simple to know. like here
$result = 'site.com'; $tags = get_meta_tags("html/".$result);
Upvotes: 1
Reputation: 1700
$html = new simple_html_dom();
$html->load_file('some_url');
//To get Meta Title
$meta_title = $html->find("meta[name='title']", 0)->content;
//To get Meta Description
$meta_description = $html->find("meta[name='description']", 0)->content;
//To get Meta Keywords
$meta_keywords = $html->find("meta[name='keywords']", 0)->content;
NOTE: The names of meta tags are casesensitive!
Upvotes: 22
Reputation: 785
$html = new simple_html_dom();
$html->load_file('http://www.google.com');
$title = $html->find('title',0)->innertext;
$html->find('title')
will return an array
so you should use $html->find('title',0)
, so does meta[description]
Upvotes: 6
Reputation: 49
$html = new simple_html_dom();
$html->load_file('xxx');
//put url or filename in place of xxx
$title = array_shift($html->find('title'))->innertext;
echo $title;
$descr = array_shift($html->find("meta[name='description']"))->content;
echo $descr;
Upvotes: 2
Reputation: 3392
I just took a look at the HTML DOM Parser, try:
$html = new simple_html_dom();
$html->load_file('xxx'); //put url or filename in place of xxx
$title = $html->find('title');
echo $title->plaintext;
$descr = $html->find('meta[description]');
echo $descr->plaintext;
Upvotes: 10