darkfriend / schema-org-parser
轻松解析HTML文档中的schema.org微数据。W3C Microdata到JSON规范的PHP实现。
1.0.0
2022-08-17 11:10 UTC
Requires
- php: >=7.0
- ext-dom: *
- ext-libxml: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- pestphp/pest: ^1.21
- phpstan/phpstan: ^1.5
Suggests
- ext-json: Needed to convert results to JSON
README
该项目是从 yusufkandemir/microdata-parser 分支出来的,以支持 php7。
本包旨在实现 W3C Microdata到JSON规范。
microdata-parser 从文档中提取微数据。
安装
通过Composer
$ composer require darkfriend/schema-org-parser
用法
PHP
use Darkfriend\SchemaOrgParser\Microdata; $microdata = Microdata::fromHTMLFile('source.html')->toJSON(); /* Other sources: fromHTML() // from HTML string fromDOMDocument() // from DOMDocument object Other output methods: toArray() // to Associtive PHP Array toObject() // to PHP Object (stdClass) */
源码(HTML)
<!-- source.html --> <div itemscope itemtype="http://schema.org/Product"> <img itemprop="image" src="http://shop.example.com/test_product.jpg" /> <a itemprop="url" href="http://shop.example.com/test_product"> <span itemprop="name">Test Product</span> </a> </div>
结果(JSON)
{
"items": [
{
"type": [ "http://schema.org/Product" ],
"properties": {
"image": [ "http://shop.example.com/test_product.jpg" ],
"url": [ "http://shop.example.com/test_product" ],
"name": [ "Test Product" ]
}
}
]
}
测试
$ composer test
贡献
请参阅 CONTRIBUTING 获取详细信息。
鸣谢
许可证
MIT许可证(MIT)。请参阅 许可证文件 获取更多信息。