/** * Enter description here... * * @param $html * @param null $contentType * @param null $documentID * @return null|string * @throws \Exception * @todo support PHP tags in input * @todo support passing \DOMDocument object from self::loadDocument */ protected static function createDocumentWrapper($html, $contentType = null, $documentID = null) { if (function_exists('domxml_open_mem')) { throw new \Exception("Old PHP4 DOM XML extension detected. PhpQuery won't work until this extension is enabled."); } // $id = $documentID // ? $documentID // : md5(microtime()); $document = null; if ($html instanceof \DOMDocument) { if (self::getDocumentID($html)) { // document already exists in PhpQuery::$documents, make a copy $document = clone $html; } else { // new document, add it to PhpQuery::$documents $wrapper = new Dom\DOMDocumentWrapper($html, $contentType, $documentID); } } else { $wrapper = new Dom\DOMDocumentWrapper($html, $contentType, $documentID); } // $wrapper->id = $id; // bind document PhpQuery::$documents[$wrapper->id] = $wrapper; // remember last loaded document PhpQuery::selectDocument($wrapper->id); return $wrapper->id; }
// 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')->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());