//" /> //" /> //"/>

PHP Querypath selector for HTML5 data attribute

57 Views Asked by At

Is it possible to use PHP querypath to get the HTML5 data attribute of a DOM element? E.g:

//HTML element
<div data-foo-attribute="whatever"></div>

//PHP
$attribute = $mydiv->attr('data-foo-attribute'); //does not work
1

There are 1 best solutions below

1
Wahyu Kristianto On BEST ANSWER

you can use attr('data-foo-attribute') for that

$html = '<div data-foo-attribute="whatever"></div>';
$qp = htmlqp($html);
$mydiv = $qp->find('div');

$attribute = $mydiv->attr('data-foo-attribute');

Or you can use data():

$html = '<div data-foo-attribute="whatever"></div>';
$qp = htmlqp($html);
$mydiv = $qp->find('div');

$attribute = $mydiv->data('foo-attribute');