Thư viện phpQuery kiểu khác (ok)
https://github.com/elboza/PhpQuery
PreviousManipulating DOM Documents with phpQueryNextDùng preg_replace thay đổi cấu trúc thẻ html (ok)
Last updated
https://github.com/elboza/PhpQuery
Last updated
C:\xampp71\htdocs\testpro\composer.json
{
"name": "phpquery/phpquery",
"description": "jquery for php",
"license": "Beerware",
"authors": [
{
"name": "Fernando Iazeolla",
"email": "fernando.iazeolla@gmail.com"
}
],
"require": {},
"autoload": {
"psr-4": {
"PhpQuery\\": "src/"
}
}
}
C:\xampp71\htdocs\testpro\test\sample.html
<!DOCTYPE html>
<html>
<head>
<title>sample html</title>
</head>
<body>
<p id="myid">Hello</p>
<div class="c1"> hello again</div>
<div class="c2"> hi</div>
<div class="c1 c2 c3">hi hi</div>
<div class="dav-k">foo</div>
<div class="Opin">
<p>bar</p>
</div>
<ul>
<li> one</li>
<li> two</li>
<li> three</li>
</ul>
</body>
</html>
C:\xampp71\htdocs\testpro\test\pqtest.php
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use PhpQuery\PhpQuery;
echo "hi<br>";
$page=file_get_contents('sample.html');
$pq=new PhpQuery;
$pq->load_str($page);
echo "<br><br>";
//return a list of 2 components
var_dump($pq->query('.c1'));
echo "<br><br>";
//return the first element
var_dump($pq->query('.c1')[0]);
echo "<br><br>";
//return a list of 1 element
var_dump($pq->query('.c1.c3'));
echo "<br><br>";
//return the first element
var_dump($pq->query('.c1.c3')[0]);
echo "<br><br>";
//return a ist of 3 elements element
var_dump($pq->query('ul li'));
echo "<br><br>";
//return a ist of 1 elements element
var_dump($pq->query('ul'));
echo "<br><br>";
//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 "<br><br>";
//return a ist of 1 elements element
var_dump($pq->query('#myid'));
echo "<br><br>";
//return the first element
var_dump($pq->query('#myid')[0]);
echo "<br>~~textContent ~~> ";
var_dump($pq->query('#myid')[0]->textContent);
echo "<br><br>";
//print the transormation fron jquery syntax
//to xpath syntax
echo $pq->j_to_x('.c1.c3');
echo $pq->j_to_x('#myid');
echo "<br><br>";
//return a list of 1 element
//from xpath syntax
var_dump($pq->xpath('//*[@id="myid"]'));
echo "<br><br>";
//return the first element
//from xpath syntax
var_dump($pq->xpath('//*[@id="myid"]')[0]);
echo '<br><br>';
var_dump($pq->innerHTML($pq->query('.Opin')[0]));
echo '<br><br>';
var_dump($pq->outerHTML($pq->query('.dav-k')[0]));
C:\xampp71\htdocs\testpro\src\PhpQuery.php
<?php
namespace PhpQuery;
class PhpQuery{
private $doc,$xpath;
function __construct(){
}
private function preprocess($str){
$list=explode(' ',$str);
return $list;
}
private function get_tag($str){
if(preg_match('/^\w+/',$str,$matches)){
return $matches[0];
}else{
return '*';
}
}
private function get_id($str){
if(preg_match('/#\w+/',$str,$matches)){
return substr($matches[0],1);
}
return '';
}
private function get_classes($str){
if(preg_match_all('/(?<=\.)[\w-]+/',$str,$matches)){
return $matches[0];
}
return array();
}
private function get_autotags($str){
if(preg_match_all('/(?<=\[)\w+=?[\"\']?\w+[\"\']?(?=\])/',$str,$matches)){
return $matches[0];
}
return array();
}
private function parse_item($str){
$item=array();
$item['tag']=$this->get_tag($str);
$item['id']=$this->get_id($str);
$item['classes']=$this->get_classes($str);
$item['autotags']=$this->get_autotags($str);
return $item;
}
private function autotags_to_xpath($autotags){
$str='';
foreach($autotags as $a){
$str .= "[@{$a}]";
}
return $str;
}
private function class_to_xpath($class){
$arr=array();
if(empty($class)) return '';
foreach($class as $a){
array_push($arr,"contains(concat(' ', normalize-space(@class), ' '), ' ${a} ')");
}
return "[" . implode(" and ",$arr) . "]";
}
private function id_to_xpath($id){
if($id==null) return '';
if($id=='') return '';
return "[@id=\"{$id}\"]";
}
public function to_xpath($item){
$str="{$item['tag']}";
$str .= $this->id_to_xpath($item['id']);
$str .= $this->autotags_to_xpath($item['autotags']);
$str .= $this->class_to_xpath($item['classes']);
return $str;
}
public function load_str($str){
$this->doc = new \DOMDocument();
$internalErrors = libxml_use_internal_errors(true);
$this->doc->loadHTML($str);
libxml_use_internal_errors($internalErrors);
$this->xpath = new \DOMXPath($this->doc);
//libxml_use_internal_errors($internalErrors);
//echo $doc->saveHTML();
}
private function free(){
$xpath=null;
$doc=null;
}
public function j_to_x($str,$b_rel=false){
$arr=array();
foreach($this->preprocess($str) as $a){
array_push($arr,$this->to_xpath($this->parse_item($a)));
}
if($b_rel==false) $rel_str='//'; else $rel_str='';
return $rel_str . implode("/",$arr);
}
public function query($str,$relative_node=null){
if($relative_node!=null) $b_rel=true; else $b_rel=false;
if($b_rel) return $this->xpath->query($this->j_to_x($str,$b_rel),$relative_node);
return $this->xpath->query($this->j_to_x($str));
}
public function xpath($str,$relative_node=null){
return $this->xpath->query($str,$relative_node);
}
public function innerHTML($element)
{
if(!$element) return'';
$innerHTML = "";
$children = $element->childNodes;
foreach ($children as $child)
{
$innerHTML .= $element->ownerDocument->saveHTML($child);
}
return $innerHTML;
}
public function outerHTML($element)
{
if(!$element) return'';
return $element->ownerDocument->saveHTML($element);
}
}