Skip to content

Latest commit

 

History

History
79 lines (57 loc) · 2.27 KB

Readme.rdoc

File metadata and controls

79 lines (57 loc) · 2.27 KB

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.

Tested with php 5.2.8

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);

SelectorDOM

Persistant object for element selection.

$dom = new SelectorDOM($html);
$links = $dom->select('a');
$list_links = $dom->select('ul li a');

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 © 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.