scotteh/php-dom-wrapper

简单的DOM包装器,用于使用CSS或XPath表达式选择节点,并快速轻松地操作结果。

3.0.0 2024-02-21 21:40 UTC

README

简介

PHP DOM Wrapper是一个简单的DOM包装器库,用于操作和遍历HTML文档。基于jQuery的操作和遍历方法,在很大程度上模仿了jQuery的行为。

作者

要求

  • PHP 8.0或更高版本
  • PSR-4兼容的自动加载器

安装

使用Composer安装。

composer require scotteh/php-dom-wrapper

自动加载

此库需要一个自动加载器,如果您还没有使用,可以包含Composers自动加载器

require 'vendor/autoload.php';

方法

操作

遍历

其他

用法

示例 #1

use DOMWrap\Document;

$html = '<ul><li>First</li><li>Second</li><li>Third</li></ul>';

$doc = new Document();
$doc->html($html);
$nodes = $doc->find('li');

// Returns '3'
var_dump($nodes->count());

// Append as a child node to each <li>
$nodes->appendWith('<b>!</b>');

// Returns: <html><body><ul><li>First<b>!</b></li><li>Second<b>!</b></li><li>Third<b>!</b></li></ul></body></html>
var_dump($doc->html());

方法

操作

addClass

self addClass(string|callable $class)
示例
$doc = (new Document())->html('<p>first paragraph</p><p>second paragraph</p>');
$doc->find('p')->addClass('text-center');

结果

<p class="text-center">first paragraph</p><p class="text-center">second paragraph</p>

follow

self follow(string|NodeList|\DOMNode|callable $input)

将参数作为同级元素直接插入到操作的每个节点之后。

示例
$doc = (new Document())->html('<ul><li>first</li><li>second</li></ul>');
$doc->find('li')->first()->follow('<li>first-and-a-half</li>');

结果

<ul>
    <li>first</li>
    <li>first-and-a-half</li>
    <li>second</li>
</ul>

appendWith

self appendWith(string|NodeList|\DOMNode|callable $input)
示例
$doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>');
$doc->find('div')->appendWith('<strong> Appended!</strong>');

结果

<div>The quick brown fox jumps over the lazy dog<strong> Appended!</strong></div>

appendTo

self appendTo(string|NodeList|\DOMNode $selector)
示例
$doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>');
$doc->create('<strong> Appended!</strong>')->appendTo('div');

结果

<div>The quick brown fox jumps over the lazy dog<strong> Appended!</strong></div>

attr

self|string attr(string $name[, mixed $value = null])
示例 #1 (设置)
$doc = (new Document())->html('<div class="text-center"></div>');
$doc->attr('class', 'text-left');

结果

<div class="text-left"></div>
示例 #2 (获取)
$doc = (new Document())->html('<div class="text-center"></div>');
echo $doc->attr('text-center');

结果

text-center

precede

self precede(string|NodeList|\DOMNode|callable $input)

将参数作为同级元素插入到操作的每个节点之前。

示例
$doc = (new Document())->html('<ul><li>first</li><li>second</li></ul>');
doc->find('li')->first()->precede('<li>zeroth</li>');

结果

<ul>
    <li>zeroth</li>
    <li>first</li>
    <li>second</li>
</ul>

clone

NodeList|\DOMNode clone()
示例
$doc = (new Document())->html('<ul><li>Item</li></ul>');
$doc->find('div')->clone()->appendTo('ul'); 

结果

<ul><li>Item</li><li>Item</li></ul>

destroy

self destroy([string $selector = null])
示例
$doc = (new Document())->html('<ul><li class="first"></li><li class="second"></li></ul>');
$doc->find('.first')->destroy();

结果

<ul><li class="second"></li></ul>

detach

NodeList detach([string $selector = null])
示例
$doc = (new Document())->html('<ul class="first"><li>Item</li></ul><ul class="second"></ul>');
$el = $doc->find('ul.first li')->detach();
$doc->first('ul.second').append($el); 

结果

<ul class="first"></ul><ul class="second"><li>Item</li></ul>

empty

self empty()
示例
$doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>');
$doc->find('div')->empty(); 

结果

<div></div>

hasClass

bool hasClass(string $class)
示例
$doc = (new Document())->html('<div class="text-center"></div>');
echo $doc->first('div')->hasClass('text-center');

结果

true

html

string|self html([string|NodeList|\DOMNode|callable $input = null])
示例 #1 (设置)
$doc = (new Document());
$doc->html('<div class="example"></div>');

结果

<div class="example"></div>
示例 #1 (获取)
$doc = (new Document())->html('<div class="example"></div>');
$doc->find('div')->appendWith('<span>Example!</span>');
echo $doc->html();

结果

<div class="example"><span>Example!</span></div>

prependWith

self prependWith(string|NodeList|\DOMNode|callable $input)
示例
$doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>');
$doc->find('div')->prependWith('<strong>Prepended! </strong>');

结果

<div><strong>Prepended! </strong>The quick brown fox jumps over the lazy dog</div>

prependTo

self prependTo(string|NodeList|\DOMNode $selector)
示例
$doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>');
$doc->create('<strong>Prepended! </strong>')->appendTo('div');

结果

<div><strong>Prepended! </strong>The quick brown fox jumps over the lazy dog</div>

removeAttr

self removeAttr(string $name)
示例
$doc = (new Document())->html('<div class="first second"></div>');
$doc->find('div').removeAttr('class');

结果

<div></div>

removeClass

self removeClass(string|callable $class)
示例
$doc = (new Document())->html('<div class="first second"></div>');
$doc->find('div').removeClass('first');

结果

<div class="second"></div>

substituteWith

self substituteWith(string|NodeList|\DOMNode|callable $input)
示例


text

string|self text([string|NodeList|\DOMNode|callable $input = null])
示例


unwrap

self unwrap()

Unwrap each current node by removing its parent, replacing the parent with its children (i.e. the current node and its siblings).

注意,每个节点都是单独操作的,因此当您在包含两个同级元素的NodeList上调用unwrap()时,将移除两个父节点。

示例
$doc = (new Document())->html('<div id="outer"><div id="first"/><div id="second"/></div>');
$doc->find('#first')->unwrap();

结果

<div id="first"></div>
<div id="second"></div>

wrap

self wrap(string|NodeList|\DOMNode|callable $input)

Wrap the current node or nodes in the given structure.

The wrapping structure can be nested, but should only contain one node on each level (any extra siblings are removed). The outermost node replaces the node operated on, while the node operated on is put into the innermost node.

If called on a NodeList, each of nodes in the list will be separately wrapped. When such a list contains multiple nodes, the argument to wrap() cannot be a NodeList or \DOMNode, since those can be used to wrap a node only once. A string or callable returning a string or a unique NodeList or \DOMNode every time can be used in this case.

When a callable is passed, it is called once for each node operated on, passing that node and its index. The callable should return either a string, or a unique NodeList or \DOMNode ever time it is called.

注意,这与所有其他方法一样,返回原始节点,而不是围绕它的(新)节点。

示例
$doc = (new Document())->html('<span>foo<span><span>bar</span>');
$doc->find->('span')->wrap('<div><p/></div>');

结果

<div><p><span>foo</span></p></div>
<div><p><span>bar</span></p></div>

wrapAll

self wrapAll(string|NodeList|\DOMNode|callable $input)

Like wrap(), but when operating on multiple nodes, all of them will be wrapped together in a single instance of the given structure, rather than each of them individually.

Note that the wrapping structure replaces the first node operated on, so if the other nodes operated on are not siblings of the first, they will be moved inside the document.

示例
$doc = (new Document())->html('<span>foo<span><span>bar</span>');
$doc->find->('span')->wrapAll('<div><p/></div>');

结果

<div><p>
    <span>foo</span>
    <span>bar</span>
</p></div>

wrapInner

self wrapInner(string|NodeList|\DOMNode|callable $input)

Like wrap(), but rather than wrapping the nodes that are being operated on, this wraps their contents.

示例
$doc = (new Document())->html('<span>foo<span><span>bar</span>');
$doc->find('span')->wrapInner('<b><i/></b>');

结果

<span><b><i>foo</i></b></span>
<span><b><i>bar</i></b></span>

遍历

add

NodeList add(string|NodeList|\DOMNode $input)

Add additional node(s) to the existing set.

示例
$nodes = $doc->find('a');
$nodes->add($doc->find('p'));

children

NodeList children()

返回当前集合中每个元素节点的所有子节点。

示例
$nodes = $doc->find('p');
$childrenOfParagraphs = $nodes->children();

closest

Element|NodeList|null closest(string|NodeList|\DOMNode|callable $input)

通过遍历当前集合中每个节点的祖先,返回与提供的输入匹配的第一个元素。

示例
$nodes = $doc->find('a');
$closestAncestors = $nodes->closest('p');

contents

NodeList contents()

返回当前集合中每个节点的所有子节点。

示例
$nodes = $doc->find('p');
$contents = $nodes->contents();

eq

\DOMNode|null eq(int $index)

返回当前集合中指定索引处的节点。

示例
$nodes = $doc->find('a');
$nodeAtIndexOne = $nodes->eq(1);

filter

NodeList filter(string|NodeList|\DOMNode|callable $input)

返回当前集合中与输入匹配的节点。

示例
$nodes = $doc->filter('a')
$exampleATags = $nodes->filter('[href*=https://example.org/]');

find

NodeList find(string $selector[, string $prefix = 'descendant::'])

根据选择器和可选的XPath轴,返回当前集合的子代节点。

示例
$nodes = $doc->find('a');

first

mixed first()

返回当前集合的第一个节点。

示例
$nodes = $doc->find('a');
$firstNode = $nodes->first();

has

NodeList has(string|NodeList|\DOMNode|callable $input)

返回当前集合的子代节点中与输入匹配的节点。

示例
$nodes = $doc->find('a');
$anchorTags = $nodes->has('span');

is

bool is(string|NodeList|\DOMNode|callable $input)

测试当前集合的节点是否与输入匹配。

示例
$nodes = $doc->find('a');
$isAnchor = $nodes->is('[anchor]');

last

mixed last()

返回当前集合的最后一个节点。

示例
$nodes = $doc->find('a');
$lastNode = $nodes->last();

map

NodeList map(callable $function)

对当前集合中的节点应用回调函数,并返回一个新的NodeList。

示例
$nodes = $doc->find('a');
$nodeValues = $nodes->map(function($node) {
    return $node->nodeValue;
});

following

\DOMNode|null following([string|NodeList|\DOMNode|callable $selector = null])

返回当前集合中每个元素节点后面的紧邻兄弟节点。

可选地通过选择器过滤。

示例
$nodes = $doc->find('a');
$follwingNodes = $nodes->following();

followingAll

NodeList followingAll([string|NodeList|\DOMNode|callable $selector = null])

返回当前集合中每个元素节点后面的所有兄弟节点。

可选地通过选择器过滤。

示例
$nodes = $doc->find('a');
$follwingAllNodes = $nodes->followingAll('[anchor]');

followingUntil

NodeList followingUntil([[string|NodeList|\DOMNode|callable $input = null], string|NodeList|\DOMNode|callable $selector = null])

返回当前集合中每个元素节点后面的所有兄弟节点,但不包括由$input匹配的节点。

可选地通过输入过滤。
可选地通过选择器过滤。

示例
$nodes = $doc->find('a');
$follwingUntilNodes = $nodes->followingUntil('.submit');

not

NodeList not(string|NodeList|\DOMNode|callable $input)

返回当前集合中不匹配输入的元素节点。

示例
$nodes = $doc->find('a');
$missingHrefAttribute = $nodes->not('[href]');

parent

Element|NodeList|null parent([string|NodeList|\DOMNode|callable $selector = null])

返回当前集合中每个元素节点的直接父节点。

可选地通过选择器过滤。

示例
$nodes = $doc->find('a');
$parentNodes = $nodes->parent();

parents

NodeList parent([string $selector = null])

返回当前集合中每个元素节点的祖先节点。

可选地通过选择器过滤。

示例
$nodes = $doc->find('a');
$ancestorDivNodes = $nodes->parents('div');

parentsUntil

NodeList parentsUntil([[string|NodeList|\DOMNode|callable $input, [string|NodeList|\DOMNode|callable $selector = null])

返回当前集合中每个元素节点的祖先节点,但不包括由$selector匹配的节点。

可选地通过输入过滤。
可选地通过选择器过滤。

示例
$nodes = $doc->find('a');
$ancestorDivNodes = $nodes->parentsUntil('div');

preceding

\DOMNode|null preceding([string|NodeList|\DOMNode|callable $selector = null])

返回当前集合中每个元素节点前面的紧邻兄弟节点。

可选地通过选择器过滤。

示例
$nodes = $doc->find('a');
$precedingNodes = $nodes->preceding();

precedingAll

NodeList precedingAll([string|NodeList|\DOMNode|callable $selector = null])

返回当前集合中每个元素节点前面的所有兄弟节点。

可选地通过选择器过滤。

示例
$nodes = $doc->find('a');
$precedingAllNodes = $nodes->precedingAll('[anchor]');

precedingUntil

NodeList precedingUntil([[string|NodeList|\DOMNode|callable $input = null], string|NodeList|\DOMNode|callable $selector = null])

返回当前集合中每个元素节点前面的所有兄弟节点,但不包括由$input匹配的节点。

可选地通过输入过滤。
可选地通过选择器过滤。

示例
$nodes = $doc->find('a');
$precedingUntilNodes = $nodes->precedingUntil('.submit');

siblings

NodeList siblings([[string|NodeList|\DOMNode|callable $selector = null])

返回当前集合中每个元素节点的兄弟节点。

可选地通过选择器过滤。

示例
$nodes = $doc->find('p');
$siblings = $nodes->siblings();

slice

NodeList slice(int $start[, int $end])

根据起始和结束索引返回当前集合的子集。

示例
$nodes = $doc->find('p');
// Return nodes 1 through to 3 as a new NodeList
$slicedNodes = $nodes->slice(1, 3);

附加方法

count

int count()
示例
$nodes = $doc->find('p');

echo $nodes->count();

each

self each(callable $function)
示例
$nodes = $doc->find('p');

$nodes->each(function($node){
    echo $node->nodeName . "\n";
});

许可

PHP DOM Wrapper由Andrew Scott许可,许可协议为BSD 3-Clause License,详细信息请参阅LICENSE文件。