简体   繁体   中英

PHP htmlentities or htmlspecialchars

I'm using fwrite to create an xml file but i'm losing the special the characters.

Example:

$message0  = htmlspecialchars('<?xml version="1.0" encoding="UTF-8"?>');
$file = fopen("test.xml","w");
echo fwrite($file,"$message0");
fclose($file);

The above code gives me the following output

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;JobTemplates&gt;

I need the special characters in order for the xml file to work. If i echo the variables, the special characters appear on the page.

Not understanding why you're encoding html characters for this. It's a trusted string, so, just put it in single quotes and write it. If any character's are giving you trouble, escape them instead of encoding them.

If there's a reason you must do it this way, then decode inline. But it all seems a bit messy to me.

Here is a tested example , you should not use htmlspecialchars

$message0  = '<?xml version="1.0" encoding="UTF-8"?><contact><name>foo</name><phone>123456</phone></contact>';
$file = fopen("test.xml","w");
fwrite($file,$message0);
fclose($file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM