dagday / xparse
本包最新版本(v0.1.1)没有可用的许可证信息。
将HTML数据解析为结构的辅助工具
v0.1.1
2021-09-12 14:12 UTC
Requires (Dev)
- nette/tester: ^2.4
- tracy/tracy: ^2.8
This package is auto-updated.
Last update: 2024-09-12 21:11:59 UTC
README
一个小型库,用于使用XPath表达式解析XML或HTML细节到PHP数组结构。
安装
$ composer require dadyday/xparse
用法
我们首先创建方案(可能在DI容器中),然后在我们的业务逻辑中解析文档。
<?php use Xparse\Parser; $xml = '<?xml version="1.0"?> <root> <el id="a" attr="an attrib"> the <b>important</b> and <i>1st</i> content </el> <el id="b"> <b>2.</b> content </el> <el id="c" attr="another attrib"> 3rd content </el> </root>'; # every el in a assoc array $oValue = Value::map('//el', # integer key of map with a default Value::int('i|b', 'none'), # value of map is a struct of values [ # short for Value::struct([...]) # a attribute with a handler function 'attr' => Value::str('@attr', function($val) { # val is null if not found return ($val ?? 'not') . ' found'; }), # all textnodes in the current context 'text' => '.', # short for Value::str('...') # returns the existence as a boolean 'next' => Value::bool('following-sibling::el'), # return a list of 2. arg within each finding of the first 'prev' => Value::list('preceding-sibling::el', '@id'), ] ); $oParser = new Parser($oValue); $aList = $oParser->parseXml($xml); $aList == [ // map for every el // key is first i-content as integer 1 => [ // struct for first el 'attr' => 'an attrib found', // attr of first el 'text' => 'the important and 1st content', // text content 'next' => true, // there are following el's 'prev' => [], // no previous el ], // key from b-content 2 => [ // secound el 'attr' => 'not found', // there is no attrib 'text' => '2. content', 'next' => true, 'prev' => ['a'], // one el before ], // key is the default 'none' 'none' => [ // third el 'attr' => 'another attrib found', 'text' => '3rd content', 'next' => false, // no el behind 'prev' => ['a', 'b'], // two el's before ], ];