darkfriend/schema-org-parser

轻松解析HTML文档中的schema.org微数据。W3C Microdata到JSON规范的PHP实现。

1.0.0 2022-08-17 11:10 UTC

This package is auto-updated.

Last update: 2024-09-17 15:31:19 UTC


README

该项目是从 yusufkandemir/microdata-parser 分支出来的,以支持 php7

Latest Version on Packagist Software License PHP Version Support Total Downloads

本包旨在实现 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)。请参阅 许可证文件 获取更多信息。