duzun / hQuery.php

An extremely fast web scraper that parses megabytes of invalid HTML in a blink of an eye. PHP5.3+, no dependencies.
https://duzun.me/playground/hquery
MIT License
361 stars 74 forks source link

Add condtions to element #39

Closed neverender24 closed 6 years ago

neverender24 commented 6 years ago

How do I filter which element is being processed?

for example, I have mutiple classes: $sels = "h2, .pnlDescription, .address";

I want to have a condition with the ".address" since I want to alter the text within it.

if(".address" == "somestring") { ----code--- }

Where and how can I achieve this one?

duzun commented 6 years ago

I would go the other way:

$h2 = $doc->find('h2');
$desc = $doc->find('.pnlDescription');
$address = $doc->find('.address');
// ... do something with these

There are still some other options, but I think they are less performant:

$sels = $doc->find('h2, .pnlDescription, .address');
foreach($sels as $e) switch(true) {
    case $el->hasClass('address'): {
        // ...
    } break;
    case $el->nodeName == 'h2': {
        // ...
    } break;
}

I would like to have ->is($selector) method, but have never had the need for it.

neverender24 commented 6 years ago

Thanks for feedback that's all I need.