php - DOMDocument > setAttribute() > bool 属性?

标签 php domdocument dom-manipulation

我尝试创建一个可以压缩 HTML 的 PHP 类,所以我想替换例如required="required"与必需。但是如何通过 DOMDocument 添加 bool 属性呢?

代码:https://3v4l.org/Ot9th

$doc = new DOMDocument("1.0");
$node = $doc->createElement("input");
$newnode = $doc->appendChild($node);
$newnode->setAttribute("required", '');
var_dump($doc->saveHTML());

结果:

<input required=""></para>

预期:

<input required></para>

最佳答案

PHP's DOMDocument创建一个有效的 XML 结构,并根据 XML 1.0XML 1.1 - 添加空属性无效。

bool 属性的值应与属性名称相同。

This is valid:

<input required="required"></para>


This is NOT valid:

<input required></para>

更新

savehtml 函数根据 dtd 验证内容。根据xhtml1.0 dtd ,这是作为空属性有效的属性列表:

checked
disabled
readonly
multiple
selected
compact
noshade
declare
ismap
nohref
nowrap

任何其他属性都必须有一个值

关于php - DOMDocument > setAttribute() > bool 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40065486/

相关文章:

php - 在搜索框中输入内容时自动显示用户名?

php - 表单发送后根据所选选项将数据库值从 0 更新为 1

PHP DOMDocument - 获取 BODY 的 html 源代码

PHP DOM 文档 : how to incorporate a string of xml?

Javascript/HTML/CSS 命运之轮仅按差异旋转?

php - Laravel 中的“格式错误的 UTF-8 字符,可能编码不正确”

php - Magento 描述/属性替换?

php - 尝试获取php中标签的值

jquery - 您可以使用 Sharepoint (MOSS 2007) 和 jQuery 安全地操作 DOM 吗?

javascript - AngularJS 指令 DOM 操作不执行 link()