Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Allow NodeList to be accessed via array like syntax. #3078

Closed
wants to merge 1 commit into from
Closed

Allow NodeList to be accessed via array like syntax. #3078

wants to merge 1 commit into from

Conversation

bayleedev
Copy link
Contributor

If we do a execute a selector and get back a NodeList and want just the first element we can now access it like so:

<?php
use Zend\Dom\Query;

$dom = new Query($html);
$results = $dom->execute('.foo .bar a');

$count = count($results); // get number of matches: 4

$results[2]->getAttribute('id'); // access individually

foreach ($results as $result) { // or iterate
    // $result is a DOMElement
}
?>

Where before you could only iterate them which is really ineffective for a lot of projects.

If we do a execute a selector and get back a NodeList and want just the first element we can now access it like so:

~~~ php
<?php
use Zend\Dom\Query;

$dom = new Query($html);
$results = $dom->execute('.foo .bar a');

$count = count($results); // get number of matches: 4

$results[2]->getAttribute('id'); // access individually

foreach ($results as $result) { // or iterate
    // $result is a DOMElement
}
?>
~~~

Where before you could only iterate them which is really ineffective for a lot of projects.
@joseym
Copy link

joseym commented Nov 27, 2012

👍

@ghost ghost assigned weierophinney Dec 10, 2012
weierophinney added a commit that referenced this pull request Dec 10, 2012
- DOMXPath::query() no longer raises an exception on failure; it now
  raises an E_WARNING. Updated code to detect an E_WARNING and raise an
  exception when discovered.
@weierophinney
Copy link
Member

Found an issue with PHP >= 5.4.1 -- DOMXPath::query() no longer throws an exception when it encounters extensions it does not know about. Updated the code to use the ErrorHandler so it can detect these situations.

Also: merged only to develop, as this is a new feature. It will release with 2.1.0.

weierophinney added a commit to zendframework/zend-dom that referenced this pull request May 15, 2015
- DOMXPath::query() no longer raises an exception on failure; it now
  raises an E_WARNING. Updated code to detect an E_WARNING and raise an
  exception when discovered.
weierophinney added a commit to zendframework/zend-dom that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants