GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - GravityPDF/querypath: A fork of QueryPath: a PHP library for HTML(5)/XML querying (CSS 4 or XPath) and processing (like jQuery) with PHP 7.1 to 8.5 support

BranchesTags
Open more actions menu

Latest commit

 

History

736 Commits
736 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
examples
examples
 
 
src
src
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
CHANGELOG.md
CHANGELOG.md
 
 
CREDITS
CREDITS
 
 
INSTALL
INSTALL
 
 
LICENSE.txt
LICENSE.txt
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
build.xml
build.xml
 
 
clover.xml
clover.xml
 
 
composer.json
composer.json
 
 
config.doxy
config.doxy
 
 
package.xml
package.xml
 
 
package_compatible.xml
package_compatible.xml
 
 
phpcompat.xml
phpcompat.xml
 
 
phpcs.xml
phpcs.xml
 
 
phpunit.xml
phpunit.xml
 
 

Repository files navigation

QueryPath: Find the better way

QueryPath is a jQuery-like library for working with XML and HTML(5) documents in PHP. It is stable software, with the original library garnering 4M+ downloads since first published in 2009.

This is a fork of a fork. The original library, and subsequent fork, are no longer being maintained. The aim of gravitypdf/querypath is to ensure the library remains compatible with the latest version of PHP, and bug free. 🧑‍💻There is still a lot of legacy code to clean up + modernize, and any assistance given is appreciated.

If you are viewing this file on QueryPath GitHub repository homepage or on Packagist, please note that the default repository branch is main which can differ from the last stable release.

Latest Stable Version License codecov PHP Version Require

Installation

composer require gravitypdf/querypath 

Basic Usage

To parse HTML or 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
}

The real power of QueryPath comes from chaining methods together. This example will generate a valid HTML5 document and output to the browser:

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
}

You can find specific nodes, loop over the matches, and extract information about each element:

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
}

See the examples directory files for more usages.

Online Manual

The legacy QueryPath manual has been automatically generated from inline DocBlocks using phpDocumentor, and can be found at http://querypath.org.

⚠️ querypath.org is not built or maintained by Gravity PDF, and we have no access to manage or change the website. Help writing new documentation in the repo's Wiki is wanted.

General Troubleshooting

For general questions or troubleshooting please use Discussions.

You can also use the querypath tag at Stack Overflow, as the StackOverflow user base is more likely to answer you in a timely manner.

Contributing

Before submitting issues and pull requests please read CONTRIBUTING.md.

If opening a Pull Request ensure the linter and PHPUnit tests pass (and write a new test for the bug you are fixing):

  • Lint: composer run lint
  • PHPUnit: vendor/bin/phpunit

About

A fork of QueryPath: a PHP library for HTML(5)/XML querying (CSS 4 or XPath) and processing (like jQuery) with PHP 7.1 to 8.5 support

Resources

Readme
License

Contributing

Contributing

Security policy

Security policy
Activity
Custom properties

Stars

31 stars

Watchers

3 watching

Forks

4 forks
Report repository

Releases

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.