2017 © Pedro Peláez
 

library phpquery

jquery for php

image

phpquery/phpquery

jquery for php

  • Saturday, April 21, 2018
  • by elboza
  • Repository
  • 1 Watchers
  • 1 Stars
  • 57 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 280 % Grown

The README.md

phpquery

jquery for php

This is a simple wrapper over the xpath library that lets you make queries on an html page with the more friendly syntax of jquery., (*1)

you can use it also to make queries with the xpath syntax, (*2)

public functions

load_str($html_string);
query($query_string,$relative_path); // returns DOMNodeList or DOMElement
xpath($query_string,$relative_path); // returns DOMNodeList or DOMElement
j_to_x($query_string);
innerHTML(DOMElement); // returns string
outerHTML(DOMElement); //return string

where $relative_path is optional and represent a saved query performed earlier., (*3)

usage

load_str($page);

$pq->query(...);


```
### example

`sample.html` file

```



sample html


Hello, (*4)

hello again
hi
hi hi
foo

bar, (*5)

  • one
  • two
  • three
``` phpquery test: ``` "; $page=file_get_contents('sample.html'); $pq=new PhpQuery; $pq->load_str($page); echo "

"; //return a list of 2 components var_dump($pq->query('.c1')); echo "

"; //return the first element var_dump($pq->query('.c1')[0]); echo "

"; //return a list of 1 element var_dump($pq->query('.c1.c3')); echo "

"; //return the first element var_dump($pq->query('.c1.c3')[0]); echo "

"; //return a ist of 3 elements element var_dump($pq->query('ul li')); echo "

"; //return a ist of 1 elements element var_dump($pq->query('ul')); echo "

"; //return a ist of 3 elements element //relative call //first lookup for ul //and the from that ul seeks li $x=$pq->query('ul'); var_dump($x=$pq->query('li',$x[0])); echo "

"; //return a ist of 1 elements element var_dump($pq->query('#myid')); echo "

"; //return the first element var_dump($pq->query('#myid')[0]); echo "
~~textContent ~~> "; var_dump($pq->query('#myid')[0]->textContent); echo "

"; //print the transormation fron jquery syntax //to xpath syntax echo $pq->j_to_x('.c1.c3'); echo $pq->j_to_x('#myid'); echo "

"; //return a list of 1 element //from xpath syntax var_dump($pq->xpath('//*[@id="myid"]')); echo "

"; //return the first element //from xpath syntax var_dump($pq->xpath('//*[@id="myid"]')[0]); echo '

'; var_dump($pq->innerHTML($pq->query('.Opin')[0])); echo '

'; var_dump($pq->outerHTML($pq->query('.dav-k')[0])); ?>

the output:, (*6)

hi


object(DOMNodeList)#6 (1) { ["length"]=> int(2) } 

object(DOMElement)#6 (18) { ["tagName"]=> string(3) "div" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(3) "div" ["nodeValue"]=> string(12) " hello again" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(3) "div" ["baseURI"]=> NULL ["textContent"]=> string(12) " hello again" } 

object(DOMNodeList)#4 (1) { ["length"]=> int(1) } 

object(DOMElement)#4 (18) { ["tagName"]=> string(3) "div" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(3) "div" ["nodeValue"]=> string(4) "hi" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(3) "div" ["baseURI"]=> NULL ["textContent"]=> string(4) "hi" } 

object(DOMNodeList)#7 (1) { ["length"]=> int(3) } 

object(DOMNodeList)#5 (1) { ["length"]=> int(1) } 

object(DOMNodeList)#9 (1) { ["length"]=> int(3) } 

object(DOMNodeList)#5 (1) { ["length"]=> int(1) } 

object(DOMElement)#5 (18) { ["tagName"]=> string(1) "p" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(1) "p" ["nodeValue"]=> string(5) "Hello" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(1) "p" ["baseURI"]=> NULL ["textContent"]=> string(5) "Hello" } 
~~textContent ~~> string(5) "Hello" 

//*[contains(@class,"c1") and contains(@class,"c3")]//*[@id="myid"]

object(DOMNodeList)#7 (1) { ["length"]=> int(1) } 

object(DOMElement)#7 (18) { ["tagName"]=> string(1) "p" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(1) "p" ["nodeValue"]=> string(5) "Hello" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(1) "p" ["baseURI"]=> NULL ["textContent"]=> string(5) "Hello" }

string(10) "

bar, (*7)

" string(28) "
foo
"

The Versions

21/04 2018

dev-master

9999999-dev

jquery for php

  Sources   Download

beerware

by Fernando Iazeolla

21/04 2018

0.0.1

0.0.1.0

jquery for php

  Sources   Download

beerware

by Fernando Iazeolla