Home > Software design >  get the content from tag by atribute
get the content from tag by atribute

Time:06-03

I need to get the content from an atribute inside a tag name a named data-copy.

This is the the non working code I got so far...

libxml_use_internal_errors(true);
$html=file_get_contents('https://mypage.com/');
$dom = new DOMDocument;
$dom->loadHTML($html);

foreach(
$dom->getElementsByTagName('a') as $thetag){
    
    
    if($thetag->getAttribute('a')=="data-copy"){echo "<h6>".$thetag->nodeValue."</h6>";}

}

CodePudding user response:

To check if an attribute is there you need to address it with it's name

$thetag->hasAttribute('data-copy')

To get the content of data-copy you can compare it like

// <a data-copy="valueoftheattribute">
$thetag->getAttribute('data-copy') === 'valueoftheattribute'

CodePudding user response:

You can also use an XPath to find the nodes directly based upon the existence of the data-copy attribute or other, more complicated criteria without needing to use hasAttribute and getAttribute like so:

$file='https://mypage.com/';

libxml_use_internal_errors( true );
$html=file_get_contents( $file );

$dom=new DOMDocument;
$dom->strictErrorChecking=false;
$dom->validateOnParse=false;
$dom->recover=true;
$dom->loadHTML( $html );
libxml_clear_errors();

$xp=new DOMXPath( $dom );
$expr='//a[@data-copy]'; # find `a` nodes anywhere in the source document that simply have the data-copy attribute


# run the query
$col=$xp->query( $expr );

# iterate through any found nodes and display the content
if( $col && $col->length > 0 ){
    foreach( $col as $i => $node )printf('<div>[%d] - %s</div>', $i, $node->nodeValue );
}
  • Related