问题描述
我从文本框中获取数据并将其更改为 xml 格式并将其存储在数据库中.为了允许特殊字符,我编写了 javascript 函数来用它的 html 实体替换特殊字符.
I am getting data from text box and change it into xml format and store it in data base. For allowing special characters i wrote javascript function to replace special character with its html entities.
" "
& &
< <
> >
对于引号,小于,大于"它的工作正常.对于&"它显示 xml 解析器错误我用javascript用它的实体替换特殊字符
for "quotes , less than , greater than" its working fine. for "&" it is showing xml parser error i used javascript to replace special character with its entity
string.replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, "\\'");
for "&" allow showing warning but it get stored in data base. please help me to sort out this problem .
i begin with string.replace(/&/g, '&') even though i am getting
警告:SimpleXMLElement::__construct():实体:第 9 行:解析器错误:EntityRef:期望 ';'在/var/www/我也试过这个 &amp;如此链接中所述stackoverflow.com/questions/1328538/...
之后没有警告,但在保存在数据库中时,它保存为ab & cd"
Warning: SimpleXMLElement::__construct(): Entity: line 9: parser error : EntityRef: expecting ';' in /var/www/ i tried this also &amp; as mentioned in this link stackoverflow.com/questions/1328538/…
After that there is no warning but while saving in db it saved as "ab & cd"
推荐答案
Start 替换 &
字符,then 替换另一个人物.否则,您将用 &
Start with replacing the &
character, then replace the other characters. Otherwise you will replace &
from the previous entities (<
etc.) by &
string.replace(/&/g, '&amp;') //<= start with
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, '&apos');
// ' may be "\\'", depends on how te OP wants to use it
[edit based on comments]使用&amp;
替换&符号
[edit based on comments] use &amp;
to replace the ampersand character
这篇关于如何解析带有特殊字符的xml?专门用于&符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!