PHP中的空白字段

时间:2018-07-17 13:32:08

标签: php xml

我想知道如何在最终用户未填写名为“名称”的表单字段并将其留为空白的情况下,将empy值返回到我的“属性”中。

我的实际代码:

$domElement = $domDocument->createElement('attribute', $posted_data['name']);
$domAttribute = $domDocument->createAttribute('domainname');
$domAttribute->value = 'Name';
$domElement->appendChild($domAttribute);

在“名称”表单字段中为空的情况下所需的输出:

<attribute domainname="Name"></attribute>

基于实际代码的当前输出:

<attribute domainname="Name"/>

如您所见,它不是由</attribute>完成,而只是由/>

完成

有任何线索吗?

1 个答案:

答案 0 :(得分:2)

您可以使用const LIBXML_NOEMPTYTAG

<?php
$domDocument = new DOMDocument;
$domElement = $domDocument->createElement('attribute');

$domAttribute = $domDocument->createAttribute('domainname');
$domAttribute->value = 'Name';

$domElement->appendChild($domAttribute);

echo $domDocument->saveXML($domElement, LIBXML_NOEMPTYTAG), PHP_EOL;

enter image description here

http://php.net/manual/en/libxml.constants.php#constant.libxml-noemptytag

相关问题