PHP DOMDocument, finding specific elements

后端 未结 2 1353
北恋
北恋 2021-01-16 02:34

I\'m looking to find a specific attribute of a specific element in an HTML document using PHP DOMDocument.

Specifically, there is a div with a unique class set, and

相关标签:
2条回答
  • 2021-01-16 03:14

    You have to use DOMXPAth class

    $doc = new DOMDocument; 
    // We don't want to bother with white spaces
    $doc->preserveWhiteSpace = false;
    
    $doc->loadHTML($htmlSource);
    
    $xpath = new DOMXPath($doc);
    
    // We starts from the root element
    $query = '//div[@class= uniqueClass]/span';
    
    $entries = $xpath->query($query);
    
    $spanStyle = $entries->current()->getAttribute('style')
    
    0 讨论(0)
  • 2021-01-16 03:16
    $xpath = new DomXPath($doc);
    $result = $xpath->evaluate('//div[@class=uniqueClass]/span/@style');
    
    0 讨论(0)
提交回复
热议问题