Giter Site home page Giter Site logo

php-selector's Introduction

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 <[email protected]>

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.

php-selector's People

Contributors

darhazer avatar lcfvs avatar tj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-selector's Issues

HTML support as well as text?

This works great but I need to get the inner HTML of an element and not just the text, is there any way to do this?
Thanks--

Broken Cyrillic

If selecting text from russian pages, the text is broken. All php code and source page in UTF-8.
broken

"br" problem

Hello I am glad to use your class but I have a problem about <br> in my code there is something <td class="xxx">hello<br>World</td> when I get this text array get
helloWorld

but I want to get
hello<br>World

How to achieve this?

Thanks in advance

:nth-child is replaced incorrectly

In selector.inc this code is wrong:

$selector = preg_replace('/([\w-]+):nth-child((\d+))/', '*/\1[position()=\2]', $selector);

[position() = \2] acts like :nth-of-type selector, not :nth-child. nth-child first selects n-th element by number and then applies all other filters. nth-of-type is applied after other filters.

So in this code nth-child should be replaced with nth-of-type.

nth-child and nth-of-type are equivalent when used without other selectors on the same level

For div:nth-child another code should be written. However I'm not experienced in xpath and can't suggest right code

incorrect selector to xpath translation

'.classa > .classb'

should be: 'descendant-or-self::*[contains(concat(" ",@class," ")," classa ")]/*[contains(concat(" ",@class," ")," classb ")]'
edit: corrected on 2015-08-26

changing
$selector = str_replace(']/*', ']', $selector);
to
$selector = str_replace(']/*[position', '][position', $selector);
fixes the issue and passes regression testing

selector 'input, button' -> fatal error

the conversion process goes something like

step 1:
input|descendant-or-self::button

step 2:
input|descendant-or-self:input[@type="button"]

which is
a) invalid
b) not what I wanted (wanted <button>, not input[type="button"])

Release 1.1.4?

Any change of making a new release, since it has been 4 years from the latest, but big fixes have since landed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.