php的 提示无效字符,PHP无效字符错误

news/2024/7/7 9:35:18

运行此代码时出现此错误:

致命错误:test.php中带有消息’无效字符错误’的未捕获异常’DOMException’:29堆栈跟踪:#0 test.php(29):DOMDocument-> createElement(‘1OhmStable’,’a’)#1 { main}在第29行的test.php中抛出

来自原始XML文件的节点确实包含无效字符,但是当我从节点中剥离无效字符时,应该创建节点.我需要在原始XML文档上进行哪种编码?我需要解码saveXML吗?

function __cleanData($c)

{

return preg_replace("/[^A-Za-z0-9]/", "",$c);

}

$xml = new DOMDocument('1.0', 'UTF-8');

$xml->load('test.xml');

$xml->formatOutput = true;

$append = array();

foreach ($xml->getElementsByTagName('product') as $product )

{

foreach($product->getElementsByTagName('name') as $name )

{

$append[] = $name;

}

foreach ($append as $a)

{

$nodeName = __cleanData($a->textContent);

$element = $xml->createElement(htmlentities($nodeName) , 'a');

}

$product->removeChild($xml->getElementsByTagName('details')->item(0));

$product->appendChild($element);

}

$result = $xml->saveXML();

$file = "data.xml";

file_put_contents($file,$result);

这就是原始XML的样子:

M100

1553725

1 Ohm Stable

600 x 1

新文档应该如下所示:

M100

1553725

<1 Ohm Stable>

1 Ohm Stable>


http://www.niftyadmin.cn/n/529808.html

相关文章

学习笔记66—兴趣阅读之哲学(佛学)

摘要: 题记&#xff1a;不阅读的人只能过一种人生&#xff0c;参与阅读的人能过成百上千的人生。&#xff08;PS&#xff1a;每读完一本书&#xff0c;一定要写读后感&#xff01;&#xff01;&#xff01;&#xff09;转载于:https://www.cnblogs.com/hechangchun/p/10495554.…

php 单行日历,PHP完整的日历类(CLASS)

class Calendar{/** www.jb51.net修正版*/var $YEAR,$MONTH,$DAY;var $WEEKarray("星期日","星期一","星期二","星期三","星期四","星期五","星期六");var $_MONTHarray("01">"一月&…

ES6小技巧笔记

1:去重数组 let a [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]let b [...new Set(a)]console.log(2222222222, b) // [1, 2, 3, 4, 5]2:合并数组let arr1 [a, b]let arr2 [c]let arr3 [d, e]let arr4 arr1.concat(arr2, arr3)let arr5 [...arr1, ...arr2, ...arr3]console.log…

php实现hmac sha1,PHP:如何生成字符串的hmac SHA1签名?

我正在尝试使用PHP连接到API并需要正确的签名.他们的文件逐字记录&#xff1a;The command string needs to hashed using HMAC SHA-1 hashing algorithmagainst the API secret key. The resulting byte array should be Base64encoded in UTF-8 format so that it can be pas…

房贷计算公式

//本息还款的月还款额(参数: 年利率/贷款总额/贷款总月份) //function getMonthMoney1(lilv,total,month){ // var lilv_month lilv / 12;//月利率 // return total * lilv_month * Math.pow(1 lilv_month, month) / ( Math.pow(1 lilv_month, m…

二分搜索树实现Java的Map(下)

二分搜索树Map public class BSTMap<K extends Comparable<K>,V> implements Map<K,V> {private class Node{public K key;public V value;public Node left,right;public Node(K key,V value){this.key key;this.value value;left null;right null;}}pr…

使用RSL来瘦身Flex生成的SWF文件

1----------------------减小应用SWF文件大小的一个方法就是将一些共享的外部资源拆分出去, 成为一个独立的文件, 这样可以单独地加载缓存到客户端. 这些共享资源可以由多个应用在运行时进行加载, 但是传递到客户端的动作只会发生一次. 这些共享文件被称为运行时共享库(Runtime…

matlab 一维 平滑,MATLAB中数据平滑处理

参考链接&#xff1a;http://bbs.pinggu.org/thread-3777396-1-1.htmlsmoothts函数调用格式&#xff1a;output smoothts(input)output smoothts(input, ‘b’, wsize) % 盒子法output smoothts(input, ‘g’, wsize, stdev) % 高斯窗方法output smoothts(input, ‘e’, n)…