31

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?

qasimzee
  • 640
  • 1
  • 12
  • 30
theotherlight
  • 783
  • 1
  • 8
  • 12
  • 3
    You're really looking for CSS3 and XPATH style selectors. JQuery and other JS libraries are based on those standardized XML selectors. – bucabay Sep 01 '09 at 20:49
  • The most up to date jQuery for PHP is https://github.com/technosophos/querypath – Petah Mar 29 '12 at 04:26

12 Answers12

37

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;
}

print_r($articles);
karim79
  • 339,989
  • 67
  • 413
  • 406
26

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.

theotherlight
  • 783
  • 1
  • 8
  • 12
  • If phpQuery answers your question you should tick this as the answer. – karim79 Sep 01 '09 at 19:54
  • I already tried, I need to wait 2 days until I can accept my own answer apparently. – theotherlight Sep 01 '09 at 20:05
  • 3
    Hey can I ask how did you decide between phpQuery and QueryPath? I want to choose one. – Positivity Dec 31 '13 at 15:18
  • Old but it still works with php 7.3.3. The project in the supplied link was [archived](https://code.google.com/archive/p/phpquery/), and there is a slightly less old repo at [Github](https://github.com/TobiaszCudnik/phpquery), which has some demo syntax. – MSpreij Feb 04 '20 at 16:27
11

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

Christian Specht
  • 35,843
  • 15
  • 128
  • 182
ali
  • 111
  • 1
  • 2
5

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

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php
$dom = new DOMDocument;
libxml_use_internal_errors(TRUE);
$dom->loadHTMLFile('http://somewhereinblog.net');

libxml_clear_errors();

$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);
}
?>
FlatLander
  • 1,717
  • 15
  • 21
4

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...

Jesús Carrera
  • 11,275
  • 4
  • 63
  • 55
2

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

Justin
  • 5,029
  • 1
  • 21
  • 21
2

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

wasinger
  • 381
  • 3
  • 7
2

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.

[http://pxtreme.sourceforge.net][1]

$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!");
});

http://pxtreme.sourceforge.net

Troy Hirni
  • 21
  • 1
1

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;
});
0

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.

Anti Veeranna
  • 11,485
  • 4
  • 42
  • 63
0

Have you looked into using PHP's DOMDocument class?

http://us2.php.net/manual/en/book.dom.php

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.

Johrn
  • 1,390
  • 7
  • 8
0

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

These components can be installed via composer.

HarryFink
  • 1,010
  • 1
  • 6
  • 6