Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Readme

  • Loading branch information...
commit 48cb89c99617d1962c9cf209211a8d9ee4f31fb0 1 parent 746e231
@tj authored
Showing with 66 additions and 4 deletions.
  1. +61 −1 Readme.rdoc
  2. +3 −3 selector.inc
  3. +2 −0  test.selector.php
View
62 Readme.rdoc
@@ -6,4 +6,64 @@ PHP data miner I had which needed more flexibility.
Current supports most CSS3 selectors.
-== Examples
+== Examples
+
+Given the sample html:
+
+ $html = <<<HTML
+ <div id="article" class="block large">
+ <h2>Article Name</h2>
+ <p>Contents of article</p>
+ <ul>
+ <li>One</li>
+ <li>Two</li>
+ <li>Three</li>
+ <li>Four</li>
+ <li><a href="#">Five</a></li>
+ </ul>
+ </div>
+ HTML;
+
+The following will return an array of elements:
+
+ select_elements('div#article.large', $html);
+ select_elements('div > h2:contains(Article)', $html);
+ select_elements('div p + ul', $html);
+ select_elements('ul > li:first-child', $html);
+ select_elements('ul > li ~ li', $html);
+ select_elements('ul > li:last-child', $html);
+ select_elements('li a[href=#]', $html);
+
+== Contribution
+
+* Like it? use it? feel free to extend and add more CSS3 support, and
+ run test.selector.php
+
+== More Information
+
+* View the source :P
+
+== License
+
+(The MIT License)
+
+Copyright (c) 2008 - 2009 TJ Holowaychuk <tj@vision-media.ca>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
6 selector.inc
@@ -7,7 +7,7 @@ define('SELECTOR_VERSION', '1.0.0');
/**
* Select elements from $html using the css $selector.
* When $as_array is true elements and their children will
- * be converted to array's containing the following keys:
+ * be converted to array's containing the following keys (defaults to true):
*
* - name : element name
* - text : element text
@@ -22,7 +22,7 @@ function select_elements($selector, $html, $as_array = true) {
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
$elements = $xpath->evaluate(selector_to_xpath($selector));
- return $as_array ? elements_to_array($elements) : $elements;
+ return $as_array ? elements_to_array($elements, $options) : $elements;
}
/**
@@ -46,7 +46,7 @@ function element_to_array($element) {
'name' => $element->nodeName,
'attributes' => array(),
'text' => $element->textContent,
- 'children' => elements_to_array($element->childNodes)
+ 'children' =>elements_to_array($element->childNodes)
);
foreach((array) $element->attributes as $key => $attr)
$array['attributes'][$key] = $attr->value;
View
2  test.selector.php
@@ -60,6 +60,7 @@ function test($selector, $expected) {
</div>
HTML;
+test_selector('*', 12);
test_selector('div', 1);
test_selector('div#article', 1);
test_selector('div#article.block', 1);
@@ -75,6 +76,7 @@ function test($selector, $expected) {
test_selector('h2:contains(Article)', 1);
test_selector('h2:contains(Article) + p', 1);
test_selector('h2:contains(Article) + p:contains(Contents)', 1);
+test_selector('div p + ul', 1);
test_selector('li ~ li', 4);
test_selector('li ~ li ~ li', 3);
test_selector('li + li', 4);
Please sign in to comment.
Something went wrong with that request. Please try again.