Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple dom php parse get custom data attribute value

HTML:

<div class="something" data-a="abc">ddsf</d>

PHP:

foreach ($dom->find('.something[data-rel]') as $this) {
    var_dump($this->attr());
}

I tried this but error. Couldn't find any info on its documentation. I want to get the data-a's value which is abc.

like image 597
user3522921 Avatar asked Jan 09 '23 16:01

user3522921


1 Answers

Why not just use the well-documented, built in, DOM extension?

Example:

$html = '<div class="something" data-a="abc">ddsf</div>';

$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

$nodes = $xpath->query('//div[@class="something"]/@data-a');
foreach ($nodes as $node) {
    var_dump($node->value);
}

Output:

string(3) "abc"
like image 55
user3942918 Avatar answered Jan 27 '23 17:01

user3942918