DADU
DADU

Reputation: 7038

PHP dom getAttribute with HTML5 attributes

How are we supposed to check the value for the following HTML5 attributes?

<input type="text" required />

Or this:

<video src="" autoplay></video>

This is the code I use:

$dom->loadHTML($html);
$xpath = new DOMXPath($dom);    
$result = $xpath->query('//input');

foreach($result as $item) var_dump($item->getAttribute('required'));

The required attribute may or may not be set, the result stays the same:

string(0) ""

If getAttribute would return null instead of an empty string when the attribute is not defined it would make more sense.

I am aware we can use something like required="required" but I can't be sure that the attribute is in that form since the code that gets parse may differ.

Upvotes: 1

Views: 721

Answers (2)

meder omuraliev
meder omuraliev

Reputation: 186562

I think the rule is that if the attribute exists, then apply the action. So try hasAttribute('required')

Upvotes: 2

Mr Coder
Mr Coder

Reputation: 8186

try

$item['required']

instead of

$item->getAttribute('required')

Upvotes: 2

Related Questions