PHP dom to get tag class with multiple css class n

2020-02-07 05:44发布

问题:

I have difficulties to get second link href and Text. How to select class="secondLink SecondClass". Using PHP Dom, Thank you

        <td class="pos" >
            <a class="firstLink" href="Search/?List=200003000112097&sr=1" >
                Firs link value
            </a>

            <br />

            <a class="secondLink SecondClass" href="/Search/?KeyOpt=ALL" >
                Second Link Value
            </a>
        </td

My code is

// parse the html into a DOMDocument
$dom = new DOMDocument();
@$dom->loadHTML($html);

/*** discard white space ***/ 
$dom->preserveWhiteSpace = false; 

// grab all the on the page

$xpath = new DOMXPath($dom);
//$hrefs = $xpath->evaluate("/html/body//a[@class='firstLink']");// its working

$hrefs = $xpath->evaluate("/html/body//a[@class='secondLink SecondClass']");// not working

Thank you

回答1:

 $hrefs = $xpath->evaluate("/html/body//a[contains(concat(' ',@class,' '),' secondClass ')
          and (contains(concat(' ',@class,' '),' secondLink '))]"

from this answer



回答2:

you can pick it by selecting your td having class pos and selecting anchor tags. then you cann control your returing array to get your specific anchor tag



标签: php css class dom