Permalink
Browse files

Added version

  • Loading branch information...
1 parent 0bd8ff6 commit 746e231df5343679b754978a87c057f6805430a7 @tj committed Jul 17, 2009
Showing with 42 additions and 0 deletions.
  1. 0 History
  2. +4 −0 History.rdoc
  3. 0 Readme
  4. +9 −0 Readme.rdoc
  5. +29 −0 selector.inc
View
0 History
No changes.
View
4 History.rdoc
@@ -0,0 +1,4 @@
+
+=== 1.0.0 / 2009-07-17
+
+* Initial Release
View
0 Readme
No changes.
View
9 Readme.rdoc
@@ -0,0 +1,9 @@
+
+= PHP Selector
+
+Quick DOM query library I whipped up for an old
+PHP data miner I had which needed more flexibility.
+
+Current supports most CSS3 selectors.
+
+== Examples
View
29 selector.inc
@@ -1,5 +1,22 @@
<?php
+// --- Selector.inc - (c) Copyright TJ Holowaychuk <tj@vision-media.ca> MIT Licensed
+
+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:
+ *
+ * - name : element name
+ * - text : element text
+ * - children : array of children elements
+ * - attributes : attributes array
+ *
+ * Otherwise regular DOMElement's will be returned.
+ */
+
function select_elements($selector, $html, $as_array = true) {
$dom = new DOMDocument();
$dom->loadHTML($html);
@@ -8,6 +25,10 @@ function select_elements($selector, $html, $as_array = true) {
return $as_array ? elements_to_array($elements) : $elements;
}
+/**
+ * Convert $elements to an array.
+ */
+
function elements_to_array($elements) {
$array = array();
for ($i = 0, $length = $elements->length; $i < $length; ++$i)
@@ -16,6 +37,10 @@ function elements_to_array($elements) {
return $array;
}
+/**
+ * Convert $element to an array.
+ */
+
function element_to_array($element) {
$array = array(
'name' => $element->nodeName,
@@ -28,6 +53,10 @@ function element_to_array($element) {
return $array;
}
+/**
+ * Convert $selector into an XPath string.
+ */
+
function selector_to_xpath($selector) {
$selector = 'descendant-or-self::' . $selector;
// :button, :submit, etc

0 comments on commit 746e231

Please sign in to comment.