开发者

PHP dom getAttribute with HTML5 attributes

开发者 https://www.devze.com 2023-02-14 03:29 出处:网络
How are we supposed to check the value for the following HTML5 attributes? <input type=\"text\" required />

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开发者_JS百科.

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.


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


try

$item['required']

instead of

$item->getAttribute('required')
0

精彩评论

暂无评论...
验证码 换一张
取 消