gravitypdf/querypath

支持PHP8.3的PHP库,用于HTML(5)/XML查询(CSS 4或XPath)和处理(类似jQuery)

4.0.0 2024-08-12 00:05 UTC

README

QueryPath是一个jQuery类似的库,用于在PHP中处理XML和HTML(5)文档。这是一个稳定的软件,自2009年首次发布以来,原始库已经获得了超过4M次下载。

这是一个分支的分支。原始库(https://github.com/technosophos/querypath)和随后的分支(https://github.com/arthurkushman/querypath)已不再维护。gravitypdf/querypath的目标是确保库与最新版本的PHP保持兼容,并且没有错误。🧑‍💻仍然有很多遗留代码需要清理和现代化,任何提供的帮助都将受到欢迎。

如果您在QueryPath GitHub仓库主页或Packagist上查看此文件,请注意默认仓库分支是main,这可能不同于最后的稳定版本。

Latest Stable Version License codecov PHP Version Require

安装

composer require gravitypdf/querypath 

基本用法

解析HTML或XML

<?php
// Assuming you installed from Composer:
require_once __DIR__.'/vendor/autoload.php';

try {
	// Recommended: uses the masterminds/html5 library to process the HTML
	$qp = html5qp(__DIR__.'/path/to/file.html'); // load a file from disk
	$qp = html5qp('<div>You can pass a string of HTML directly to the function</div>'); // load a string
} catch (\QueryPath\Exception $e) {
	// Handle error
}

try {
	// Legacy: uses libxml to parse HTML
	$qp = htmlqp(__DIR__.'/path/to/file.html'); // load a file from disk
	$qp = htmlqp('<div>You can pass a string of HTML directly to the function</div>'); // load a string
} catch (\QueryPath\Exception $e) {
	// Handle error
}

try {
	// XML or XHTML
	$qp = qp(__DIR__.'/path/to/file.html'); // load a file from disk
	$qp = qp("<?xml version='1.0'?><hello><world/></hello>"); // load a string
} catch (\QueryPath\Exception $e) {
	// Handle error
}

QueryPath真正的力量来自于方法链。以下示例将生成有效的HTML5文档并输出到浏览器

try {
	html5qp(\QueryPath\QueryPath::HTML5_STUB, 'title')
		// Add some text to the title
		->text('Example of QueryPath.')
		// Now look for the <body> element
		->top('body')
		// Inside the body, add a title and paragraph.
		->append('<h1>This is a test page</h1><p>Test text</p>')
		// Now we select the paragraph we just created inside the body
		->children('p')
		// Add a 'class="some-class"' attribute to the paragraph
		->attr('class', 'some-class')
		// And add a style attribute, too, setting the background color.
		->css('background-color', '#eee')
		// Now go back to the paragraph again
		->parent()
		// Before the paragraph and the title, add an empty table.
		->prepend('<table id="my-table"></table>')
		// Now let's go to the table...
		->top('#my-table')
		// Add a couple of empty rows
		->append('<tr></tr><tr></tr>')
		// select the rows (both at once)
		->children()
		// Add a CSS class to both rows
		->addClass('table-row')
		// Now just get the first row (at position 0)
		->eq(0)
		// Add a table header in the first row
		->append('<th>This is the header</th>')
		// Now go to the next row
		->next()
		// Add some data to this row
		->append('<td>This is the data</td>')
		// Write it all out as HTML
		->writeHTML5();
} catch (\QueryPath\Exception $e) {
	// Handle error
}

您可以找到特定的节点,遍历匹配项,并提取每个元素的信息

try {
	$html = '
    <ul>
        <li>Foo</li>
        <li>Bar</li>
        <li>FooBar</li>
    </ul>';

	$qp = html5qp($html);
	foreach ($qp->find('li') as $li) {
		echo $li->text() .'<br>';
	}
} catch (\QueryPath\Exception $e) {
	// Handle error
}

有关更多用法,请参阅示例目录文件

在线手册

遗留的QueryPath手册已通过phpDocumentor自动从行内DocBlocks生成,并可在http://querypath.org找到。

⚠️ querypath.org不是由Gravity PDF构建或维护的,我们没有权限管理或更改该网站。我们希望在repo的Wiki中编写新的文档

一般故障排除

有关一般问题或故障排除,请使用讨论

您还可以在Stack Overflow上使用querypath标签,因为StackOverflow用户基础更有可能在第一时间回答您。

贡献

在提交问题和拉取请求之前,请阅读CONTRIBUTING.md

在打开拉取请求时,请确保通过代码检查器(linter)和PHPUnit测试(并为您正在修复的bug编写一个新的测试)

  • 代码检查器:composer run lint
  • PHPUnit:vendor/bin/phpunit