首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

phpQuery:讓 PHP 解析 HTML 像 jQuery 一樣簡單

     編輯:PHP 相關知識

phpQuery:讓 PHP 解析 HTML 像 jQuery 一樣簡單

在 PHP 中解析 HTML 文檔一直是開發者的痛點。正則表達式脆弱難維護,DOMDocument API 繁瑣。phpQuery 移植了 jQuery 的選擇器語法和鏈式操作風格,讓服務端 HTML 解析變得極其直觀高效。

項目資源

快速開始

引入單文件版:

<?php
include('phpQuery-onefile.php');
?>

實戰示例:提取遊戲列表數據

假設有 test.html 包含以下結構:

<div class="thumb" id="Thumb-13164-3640">
    <a href="/Spiderman-City-Drive">
        <img src="/thumb/12/Spiderman-City-Drive.jpg" alt="">
        <span class="GameName" id="GameName-13164-3640">Spiderman City Drive</span>
        <span class="GameRating" id="GameRating-13164-3640">
            <span></span>
        </span>
    </a>
</div>
<div class="thumb" id="Thumb-13169-5946">
    <a href="/Spiderman-City-Raid">
        <img src="/thumb/12/Spiderman-City-Raid.jpg" alt="">
        <span class="GameName" id="GameName-13169-5946">Spiderman - City Raid</span>
        <span class="GameRating" id="GameRating-13169-5946">
            <span></span>
        </span>
    </a>
</div>

PHP 解析代碼:

<?php
include('phpQuery-onefile.php');

$filePath = 'test.html';
$fileContent = file_get_contents($filePath);
$doc = phpQuery::newDocumentHTML($fileContent);
phpQuery::selectDocument($doc);

$data = array(
    'name' => array(),
    'href' => array(),
    'img'  => array()
);

// 提取所有鏈接 href
foreach (pq('a') as $t) {
    $href = $t->getAttribute('href');
    $data['href'][] = $href;
}

// 提取所有圖片 src
$domain = 'https://www.gahe.com'; // 域名前綴
foreach (pq('img') as $img) {
    $data['img'][] = $domain . $img->getAttribute('src');
}

// 提取遊戲名稱(通過 nodeValue 獲取文本內容)
foreach (pq('.GameName') as $name) {
    $data['name'][] = $name->nodeValue;
}

var_dump($data);
?>

輸出結果

array (size=3)
  'name' => 
    array (size=2)
      0 => string 'Spiderman City Drive' (length=20)
      1 => string 'Spiderman - City Raid' (length=21)
  'href' => 
    array (size=2)
      0 => string 'https://www.gahe.com/Spiderman-City-Drive' (length=40)
      1 => string 'https://www.gahe.com/Spiderman-City-Raid' (length=39)
  'img' => 
    array (size=2)
      0 => string 'https://www.gahe.com/thumb/12/Spiderman-City-Drive.jpg' (length=53)
      1 => string 'https://www.gahe.com/thumb/12/Spiderman-City-Raid.jpg' (length=52)

核心優勢

  • jQuery 風格選擇器:支持 pq('div.thumb a')、pq('#id')、pq('.class') 等完整 CSS 選擇器
  • 鏈式操作:pq('div')->find('a')->attr('href')
  • DOM 操作:支持 append、prepend、remove、attr、html、text 等完整 API
  • 文檔切換:phpQuery::selectDocument($doc) 管理多文檔上下文

常用 API 速查

操作代碼示例
創建文檔phpQuery::newDocumentHTML($html)
從文件加載phpQuery::newDocumentFileHTML('test.html')
選擇元素pq('.class'), pq('#id'), pq('div > a')
獲取屬性$el->getAttribute('href') 或 pq($el)->attr('href')
獲取文本$el->nodeValue 或 pq($el)->text()
獲取 HTMLpq($el)->html() 或 pq($el)->htmlOuter()
查找子元素pq($el)->find('span')
遍歷foreach (pq('a') as $el) { ... }

注意事項

  • phpQuery 基於 DOMDocument,需啟用 php_dom 和 php_libxml 擴展
  • 大文檔解析可能消耗較多內存,注意內存限制
  • HTML5 標籤在舊版本中可能解析異常,建議使用較新版本
  • 項目已多年未更新,新項目可考慮 Symfony DomCrawler 或 PHP HTML Parser

phpQuery 以其極低的學習成本和強大的功能,成為 PHP 爬蟲、內容提取、模板渲染等場景的利器。對於熟悉 jQuery 的開發者幾乎零成本上手。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved