cshaptx4869 / phpquery
此包已被废弃,不再维护。未建议替代包。
基于 https://code.google.com/archive/p/phpquery/
v0.1.0
2019-08-11 06:37 UTC
Requires
- php: >5.0
This package is auto-updated.
Last update: 2023-09-26 02:39:13 UTC
README
https://code.google.com/archive/p/phpquery/ 项目的 composer 化
phpQuery 是一个基于 jQuery JavaScript 库 的服务器端、可链式操作、CSS3 选择器驱动的文档对象模型 (DOM) API。
库是用 PHP5 编写的,并提供额外的 命令行接口 (CLI)。
安装
composer require cshaptx4869/phpQuery
示例
<?php require_once 'vendor/autoload.php'; // INITIALIZE IT // phpQuery::newDocumentHTML($markup); // phpQuery::newDocumentXML(); // phpQuery::newDocumentFileXHTML('test.html'); // phpQuery::newDocumentFilePHP('test.php'); // phpQuery::newDocument('test.xml', 'application/rss+xml'); // this one defaults to text/html in utf8 $doc = phpQuery::newDocument('<div/>'); // FILL IT // array syntax works like ->find() here $doc['div']->append('<ul></ul>'); // array set changes inner html $doc['div ul'] = '<li>1</li> <li>2</li> <li>3</li>'; // MANIPULATE IT $li = null; // almost everything can be a chain $doc['ul > li'] ->addClass('my-new-class') ->filter(':last') ->addClass('last-li') // save it anywhere in the chain ->toReference($li); // SELECT DOCUMENT // pq(); is using selected document as default phpQuery::selectDocument($doc); // documents are selected when created or by above method // query all unordered lists in last selected document $ul = pq('ul')->insertAfter('div'); // ITERATE IT // all direct LIs from $ul foreach($ul['> li'] as $li) { // iteration returns PLAIN dom nodes, NOT phpQuery objects $tagName = $li->tagName; $childNodes = $li->childNodes; // so you NEED to wrap it within phpQuery, using pq(); pq($li)->addClass('my-second-new-class'); } // PRINT OUTPUT // 1st way print phpQuery::getDocument($doc->getDocumentID()); // 2nd way print phpQuery::getDocument(pq('div')->getDocumentID()); // 3rd way print pq('div')->getDocument(); // 4th way print $doc->htmlOuter(); // 5th way print $doc; // another... print $doc['ul'];