国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

phpQuery讓php處理html代碼像jQuery一樣方便

2024-05-04 23:29:51
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了phpQuery讓php處理html代碼像jQuery一樣方便,需要的朋友可以參考下
 

簡介

如何在php中方便地解析html代碼,估計是每個phper都會遇到的問題。用phpQuery就可以讓php處理html代碼像jQuery一樣方便。

項目地址:https://code.google.com/p/phpquery/

github地址:https://github.com/TobiaszCudnik/phpquery

DEMO

下載庫文件:https://code.google.com/p/phpquery/downloads/list

我下的是onefile版:phpQuery-0.9.5.386-onefile.zip

官方demo:https://code.google.com/p/phpquery/source/browse/branches/dev/demo.php

然后在項目中引用。

html文件test.html

 

復制代碼代碼如下:

<div class="thumb" id="Thumb-13164-3640" style="position: absolute; left: 0px; top: 0px;">
    <a href="/Spiderman-City-Drive">
        <img src="/thumb/12/Spiderman-City-Drive.jpg" style="border-left-color: rgb(0, 153, 204); border-left-width: 1px; border-left-style: solid; padding: 0px 3px; margin: 3px auto 0px; width: 640px; background-color: rgb(242, 246, 251); clear: both; border-top-color: rgb(0, 153, 204); border-top-width: 1px; border-top-style: solid; border-right-color: rgb(0, 153, 204); border-right-width: 1px; border-right-style: solid;"> 復制代碼代碼如下:

<?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()
    );
    foreach (pq('a') as $t) {
        $href = $t -> getAttribute('href');
        $data['href'][] = $href;
    }
    foreach (pq('img') as $img) {
        $data['img'][] = $domain . $img -> getAttribute('src');
    }
    foreach (pq('.GameName') as $name) {
        $data['name'][] = $name -> nodeValue;
    }
    var_dump($data);
?>

 

上面的代碼中包含了取屬性和innerText內容(通過nodeValue?。?。

輸出:

 

復制代碼代碼如下:

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 'http://www.gahe.com/Spiderman-City-Drive' (length=40)
      1 => string 'http://www.gahe.com/Spiderman-City-Raid' (length=39)
  'img' => 
    array (size=2)
      0 => string 'http://www.gahe.com/thumb/12/Spiderman-City-Drive.jpg' (length=53)
      1 => string 'http://www.gahe.com/thumb/12/Spiderman-City-Raid.jpg' (length=52)

 

強大的是pq選擇器,語法類似jQuery,很方便。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 周至县| 凤阳县| 贵溪市| 措勤县| 德州市| 舟山市| 宾阳县| 长寿区| 岳阳市| 岗巴县| 成安县| 平乐县| 原阳县| 利川市| 南川市| 湖北省| 江西省| 自贡市| 长宁县| 七台河市| 白城市| 哈尔滨市| 阿克苏市| 青海省| 怀集县| 曲水县| 芦溪县| 孝昌县| 边坝县| 吐鲁番市| 阜康市| 商洛市| 哈密市| 汽车| 垣曲县| 九台市| 什邡市| 青铜峡市| 溧阳市| 祁连县| 黔南|