承接 dadyday/xparse 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dadyday/xparse

Composer 安装命令:

composer require dadyday/xparse

包简介

Helper to parse html data into a struct

README 文档

README

A small lib, to parse XML or HTML details with XPath expressions into a PHP array structure.

Installation

$ composer require dadyday/xparse

Usage

We create the scheme first (maybe in a DI container), and parse the document within our business logic.

<?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
    ],
];

统计信息

  • 总下载量: 18
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: HTML

其他信息

  • 授权协议: Unknown
  • 更新时间: 2021-09-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固