CodeIgniter: A Class/Library to help get meta tags from a web page?

馋奶兔 提交于 2019-11-26 14:42:51

问题


I am using codeigniter. I guess it doesn't matter which php framework I am using.

But before I write my own class is there another that has already been written that allows a user to get the page title and meta tags (keywords, descriptions) of any sit...if they have any.

Any sort of PHP class that does that would be great.

Thanks all


回答1:


You should have a look at this class: PHP Simple HTML DOM it works this way:

include('simple_html_dom.php');
$html = file_get_html('http://www.codeigniter.com/');

echo $html->find('title', 0)->innertext; // get <title>

echo "<pre>";
foreach($html->find('meta') as $element)
       echo $element->name . " : " . $element->content  . '<br>'; //prints every META tag

echo "</pre>";



回答2:


You can get all the meta tags froma remote page with get_meta_tags - http://ca3.php.net/get_meta_tags

this page has a class to get the page and description, they are also using get_meta_tags - http://www.emirplicanic.com/php/get-remote-page-title-with-php.php

You should be able to combine bits from both to get everything you need.




回答3:


Use PHP's curl library. It can pull other pages from the web and fetch them as strings, and then you can parse the string with regular expressions to find the page's title and meta tags.




回答4:


See this please. This is generic class to get page meta tags and do a lot more. See if you can add this in codeigniter library. Thanks




回答5:


With DOM/xpath

libxml_use_internal_errors(true);
$c = file_get_contents("http://url/here");
$d = new DomDocument();
$d->loadHTML($c);
$xp = new domxpath($d);
foreach ($xp->query("//meta[@name='keywords']") as $el) {
    echo $el->getAttribute("content");
}
foreach ($xp->query("//meta[@name='description']") as $el) {
    echo $el->getAttribute("content");
}


来源:https://stackoverflow.com/questions/2273555/codeigniter-a-class-library-to-help-get-meta-tags-from-a-web-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!