[phpquery/phpquery]PHP中的JQuery,像JQ一样处理HTML

2022-03-26 奥古斯宏

基本函数

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

基本用法

<?php 

require_once __DIR__ . '/../vendor/autoload.php';
use PhpQuery\PhpQuery

$page=file_get_contents('sample.html');
$pq=new PhpQuery;
$pq->load_str($page);

$pq->query(...);

实例

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>

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]));

打印内容

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) "<p>bar</p>"

string(28) "<div class="dav-k">foo</div>"


最近浏览
IP用户:49.51.*.*
2 天前 Mobile Safari iOS 13.2
IP用户:66.249.*.*
3 天前 Googlebot
IP用户:111.25.*.*
3 天前 Internet Explorer Windows CE
IP用户:112.13.*.*
3 天前 Generic Bot
IP用户:114.119.*.*
6 天前 Petal Bot
IP用户:3.224.*.*
8 天前 Amazon Bot
IP用户:60.188.*.*
8 天前 Generic Bot
IP用户:45.55.*.*
9 天前 Chrome Windows 10
IP用户:60.188.*.*
12 天前 Generic Bot
IP用户:52.167.*.*
15 天前 BingBot
IP用户:39.173.*.*
15 天前 Generic Bot
IP用户:119.13.*.*
16 天前 Opera Windows Server 2003
累计浏览次数:1458
评论
点击登录
phpreturn,PHP武器库,专注PHP领域的项目和资讯,收录和介绍PHP相关项目。
最近浏览 点击登录
累计浏览次数:190884
一周浏览次数:1134
今日浏览次数:156

本站所有权利归 phpreturn.com 所有

举报/反馈/投稿邮箱:phpreturn@ulthon.com

鲁ICP备19027671号-2