wpillar/simple-dom

PHP 对 DOMElement 的包装器。

dev-master 2014-11-23 12:34 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:17:59 UTC


README

Build Status SensioLabsInsight Scrutinizer Code Quality PHP Version

PHP 的 DOMElement 类的包装器,提供了一个更友好的 API 来从元素中检索数据。

安装

您可以选择任何您喜欢的安装方式,但我们推荐使用 Composer

{
    "require": {
        "wpillar/simple-dom": "dev-master"
    }
}

使用

XML

$xml = \Pillar\SimpleDom\Element::xml('<?xml version="1.0"?>
    <Response>
        <Items>
            <Item>
                <ASIN>1477825274</ASIN>
                <ItemAttributes>
                    <Title>From the Cradle</Title>
                    <Actor>Brad Pitt</Actor>
                    <Actor>Angelina Jolie</Actor>
                </ItemAttributes>
            </Item>
        </Items>
    </Response>
');

foreach ($xml->getElements('Items') as $item) {
    $item->getValue('ASIN'); // 1477825274
    
    $item->getElement('ItemAtrributes'); // Pillar\SimpleDom\Element
    $item->getElement('ItemAttributes')->getValue('Title'); // 'From the Cradle'
    $item->getElement('ItemAttributes')->getValue('Actor'); // 'Brad Pitt'
    $item->getElement('ItemAttributes')->getValues('Actor'); // ['Brad Pitt', 'Angelina Jolie']
}

HTML

$html = \Pillar\SimpleDom\Element::html('<!DOCTYPE html>
    <html>
        <head>
            <title>My special website</title>
        </head>
        <body>
            <h1>Welcome to My Special Website!</h1>
            <p>Hello World!</p>
            <p>My name is Will</p>
        </body>
    </html>
');

$html->getElement('head')->getValue('title'); // 'My special website'
$html->getElement('body')->getValue('h1'); // 'Welcome to My Special Website'
$html->getElement('body')->getValue('p'); // 'Hello World!'
$html->getElement('body')->getValues('p'); // ['Hello World!', 'My name is Will']
$html->getElements('p'); // Pillar\SimpleDom\Element[]

贡献

我更喜欢使用 GitHub Flow 进行工作。因此,请提交包含您建议更改的 PR,并说明添加更改的理由,以及任何适当的文档和测试。

此库旨在遵循 PSR-1PSR-2PSR-4 标准,因此请确保您的贡献也符合这些标准。另外,如果您发现任何现有的标准违反,欢迎提交修复这些问题的 PR。

许可证

MIT 许可证 (MIT)

版权所有 (c) 2014 Will Pillar

特此免费授予任何人获得本软件及其相关文档文件(以下简称“软件”)副本的权利,允许在不限制的情况下处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许向软件提供副本的人进行上述操作,但受以下条件约束

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“现状”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于适销性、特定用途的适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论此类责任是因合同、侵权或其他行为引起的,无论此类责任是源于、因或与软件或软件的使用或其他操作有关。