PHP Classes

File: test.php

Recommend this page to a friend!
  Classes of Gonzalo Chumillas   PHP CSS Parser   test.php   Download  
File: test.php
Role: Example script
Content type: text/plain
Description: Examples
Class: PHP CSS Parser
Get HTML document nodes matching a CSS selector
Author: By
Last change: commit
commit
Merge branch 'master' of https://github.com/cequiel/cssparser
update
Date: 10 years ago
Size: 1,433 bytes
 

Contents

Class file image Download
<?php
header
("Content-Type: text/plain; charset=UTF-8");
require_once
"classes/css-parser/css-parser.php";

// loads an xml document
$doc = new DOMDocument("1.0", "UTF-8");
$doc->load('test.xml');

// equal attribute filter example
$query = "books item[author_id = 'eduardo-mendoza']";
echo
"** Select \"$query\":\n";
$nodes = CSSHelper::select($doc, $query);
foreach (
$nodes as $node) {
    echo
CSSHelper::dom2str($node) . "\n~~~\n";
}
echo
"\n";

// id filter example
$query = "books item#7";
echo
"** Select \"$query\":\n";
$nodes = CSSHelper::select($doc, $query);
foreach (
$nodes as $node) {
    echo
CSSHelper::dom2str($node) . "\n~~~\n";
}
echo
"\n\n";

// pseudo filter example
$query = "books item:nth-child(2)";
echo
"** Select \"$query\":\n";
$nodes = CSSHelper::select($doc, $query);
foreach (
$nodes as $node) {
    echo
CSSHelper::dom2str($node) . "\n~~~\n";
}
echo
"\n";

// multiple filters example
$query = "books .sci-fi[author_id = isaac-asimov][id != 5][lang |= en]";
echo
"** Select \"$query\":\n";
$nodes = CSSHelper::select($doc, $query);
foreach (
$nodes as $node) {
    echo
CSSHelper::dom2str($node) . "\n~~~\n";
}
echo
"\n\n";


// multiple selectors example
$query = "books .comedy[author_id = eduardo-mendoza], books .sci-fi:nth-child(2)";
echo
"** Select \"$query\":\n";
$nodes = CSSHelper::select($doc, $query);
foreach (
$nodes as $node) {
    echo
CSSHelper::dom2str($node) . "\n~~~\n";
}
echo
"\n\n";