donglanying3855 2011-02-28 17:57
浏览 22
已采纳

带有HTML5属性的PHP dom getAttribute

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.

  • 写回答

2条回答 默认 最新

  • doutun1362 2011-02-28 18:05
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址