Home » Php » jQuery-like interface for PHP?

jQuery-like interface for PHP?

Posted by: admin April 23, 2020 Leave a comment


I was curious as to whether or not there exists a jQuery-style interface/library for PHP for handling HTML/XML files — specifically using jQuery style selectors.

I’d like to do things like this (all hypothetical):

foreach (j("div > p > a") as anchor) {
   // ...

print j("#some_id")->html();

print j("a")->eq(0)->attr("name");

These are just a few examples.

I did as much Googling as I could but couldn’t find what I was looking for. Does anyone know if something along these lines exist, or is this something I’m going to have to make from scratch myself using domxml?

How to&Answers:

PHP Simple HTML DOM Parser uses jQuery-style selectors. Examples from the documentation:

Modifying HTML elements:

// Create DOM from string
$html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>');

$html->find('div', 1)->class = 'bar';

$html->find('div[id=hello]', 0)->innertext = 'foo';

echo $html; // Output: <div id="hello">foo</div><div id="world" class="bar">World</div>

Scraping Slashdot:

// Create DOM from URL
$html = file_get_html('http://slashdot.org/');

// Find all article blocks
foreach($html->find('div.article') as $article) {
    $item['title']     = $article->find('div.title', 0)->plaintext;
    $item['intro']    = $article->find('div.intro', 0)->plaintext;
    $item['details'] = $article->find('div.details', 0)->plaintext;
    $articles[] = $item;



Doing some more hunting, I think I might’ve found precisely what I was looking for:

phpQuery – jQuery port to PHP

Thanks everyone for your answers, I will definitely keep them in mind for other uses.


The question is old but what you need is Query Path.


Trust me you are looking for xPath. I am showing you an example

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
$dom = new DOMDocument;


$xPath = new DOMXPath($dom);
$links = $xPath->query('//h1//a'); //This is xPath. Really nice and better than anything
foreach($links as $link) {
    printf("<p><a href='%s'>%s</a></p>\n", $link->getAttribute('href'), $link->nodeValue);


The best one I found is https://github.com/scotteh/php-dom-wrapper

It works very similarly as jQuery, and it is fast.

I tried many libraries from other answers, but I didn’t manage to port the manipulations I was doing in jQuery easily. With this one it was a breeze. I guess it will become more popular soon…


http://fluentdom.org/ is another alternative.


I wrote a library that duplicates jQuery’s DOM manipulation methods in PHP, but it uses xpath, not the jquery style selectors. Otherwise, it works pretty much the same.

$doc = px("index.html"); // Create a px Object
$headings=$doc->xpath("/html/body/h2"); // Select Elements to Manipulate
$headings->addClass("NewLook"); // Change their Appearance
px("index.html")->xpath("//h2")->addClass("NewLook"); // All in One Line

// use anonymous functions in PHP 5.3
$doc->xpath("//p")->each( function ($pxObject, $index) {
  $str = $pxObject->get($index)->text();
  if (mb_strpos($str, "pxtreme"))
   $px->attr("title", "Check out this paragraph!");



HtmlPageDom extends Symfony’s DOM Crawler and adds jQuery-like DOM manipulation functions.


my alternative is https://github.com/gymadarasz/xparser

fast and easy to use, an example:

$x('#nav a', function($elem) {
  $elem->href = '//myurl/' . $elem->href;


simplexml perhaps? Its syntax is different from jquery, but it does make traversing XML really easy.

It will however not work for HTML that is not valid XML.


Have you looked into using PHP’s DOMDocument class?


Not sure if this is exactly what you’re looking for, but it does allow for searching a document by various attributes, and other such DOM manipulation.


If you use a modern framework, you should check these out too.

These components can be installed via composer.