yusufkandemir/microdata-parser

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

v1.0.1 2024-05-01 16:14 UTC

This package is auto-updated.

Last update: 2024-08-31 00:26:30 UTC


README

Latest Version on Packagist PHP Version Support Software License Tests Quality Checks Total Downloads

此包旨在实现W3C Microdata到JSON规范

microdata-parser 从文档中提取微数据。

安装

通过Composer

$ composer require yusufkandemir/microdata-parser

用法

PHP
use YusufKandemir\MicrodataParser\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)。请参阅许可文件以获取更多信息。