10

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.

1

9 Answers 9

22
$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!

1
  • Spot on. +1 for grabbing by tag and attribute. Apr 2, 2013 at 5:43
10

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;
2
  • 4
    This code does not work for me (anymore? --- the answer is older than the latest version of the library), because find may return more than one element. To get it working I need to add a second parameter with a value of 0 to find: $html->find('title', 0)->plaintext;
    – BlaM
    Apr 1, 2014 at 14:42
  • the answer is incorrect - see my tested answer below Mar 29, 2016 at 8:05
6
$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]

4

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;
}
3
$html->find('meta[name=keywords]',0)->attr['content'];
$html->find('meta[name=description]',0)->attr['content'];
2
$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;
2
  • yes! tested this code . look myblog
    – liuqing
    Sep 25, 2012 at 6:44
  • I get a warning: Strict Standards: Only variables should be passed by reference
    – rebellion
    Nov 6, 2013 at 17:52
1

you can using php code and so simple to know. like here

$result = 'site.com'; $tags = get_meta_tags("html/".$result);

1
  • this function fails horribly in some cases Mar 29, 2016 at 8:11
1

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

1
  • Which var should hold the web URL? Sep 13, 2016 at 9:44
0

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);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.