piradoiv / munchitos
像老板一样对象化HTML。
1.1.0
2014-03-04 22:42 UTC
Requires
- symfony/css-selector: 2.4.*
- symfony/dom-crawler: 2.4.*
- webignition/absolute-url-deriver: 1.4.*
Requires (Dev)
- phpunit/phpunit: 3.7.*
- squizlabs/php_codesniffer: 1.5.*
This package is not auto-updated.
Last update: 2024-09-28 16:15:21 UTC
README
这个库旨在使开发者解析HTML内容时生活更轻松。
特性
目前,Munchitos具有以下特性
- 返回页面标题。
- 从内容中提取链接和图片。
- 检测元描述。
- 如果有,则返回规范URL。
- 列出每个链接样式表。
- 检测字符集编码。
将来,Munchitos旨在支持
- 语义微格式。
如何安装
Composer是最简单的方法,只需将库添加到依赖项
{
"require": {
"piradoiv/munchitos": "dev-master"
}
}
记得在添加composer.json文件后调用 composer install 或 composer update。有关在项目中设置 Composer 的信息有很多。
Munchitos的目标不是下载HTML本身,所以我建议还安装一个cURL包装器,如 shuber/curl。
如何使用
<?php // First you'll have to load composer and create // a Munchitos instance. require 'vendor/autoload.php'; $munchitos = new PiradoIV\Munchitos\Munchitos; // Optionally but definitely you want to do it, // set the source URL from where you downloaded // the HTML contents. $munchitos->url("http://www.example.com/foo/bar.html"); // Fill it with the HTML $html = '<html><body><p>Hello World!</p></body></html>'; $munchitos->html($html); // And start calling any information you need. echo $munchitos->title();
$munchitos->title()
返回页面标题,去除空格。
$munchitos->description()
如果有任何元描述标签,则返回其内容。
$munchitos->canonical()
一些网站指定了规范URL,在那里。
$munchitos->stylesheets()
返回当前HTML上使用的样式表URL列表(如果有)。
$munchitos->charset()
搜索字符集标签并返回其内容。
$munchitos->links()
返回一个包含Link实例的数组。Link类包含以下有用的方法
- href()
- title()
- target()
- isNoFollow() / isFollow()
$munchitos->images()
返回一个包含Image实例的数组。与Link类一样,它包含一些方法
- altText() 或 alt()
- src()
- isLinked()
致谢
我想感谢开源社区,特别是Composer团队、Symfony库和PHP League,他们的项目模板。
贡献
只需发送一个pull request,但请确保您的代码不会破坏测试,并且编写时使用PSR-2编码风格。
联系
我在Twitter上是 @PiradoIV,如果您需要这个库的帮助,请给我留言。