问题
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.
" "
& &
< <
> >
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
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"
回答1:
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] use &amp;
to replace the ampersand character
来源:https://stackoverflow.com/questions/11555890/how-to-parse-xml-with-special-character-specifically-for-ampersand