note 74564 deleted from function.utf8-encode by cmb
| From: | cmb@php.net | Date: | Sun, 22 Dec 2019 08:57:55 +0000 |
| Subject: | note 74564 deleted from function.utf8-encode by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213570@lists.php.net to get a copy of this message | ||
Note Submitter: penda ekoka
----
creating utf-8 xml files:
this is something that has wasted a lot of my time, I hope this will spare you the headaches:
my method consists of creating an xml template that will look like this (this is probably optional,
I'm sure you can use good ol' print or echo statements):
xml_tpl.php
<?php
header("Content-Type: text/html;charset=ISO-8859-1");
print "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
$names=array('jack','bob','vanessa','catherine','valerie');
?>
<parent>
<?php foreach($names as $name) {?>
<child name="<?php print $name?>" />
<?php } ?>
</parent>
?>
from a function or a method I include the previous template and trap the outputted content in an
output buffer. The buffured content is then inserted into a file:
<?php
function create_xml(){
ob_start();
include "xml_php.php";
$trapped_content=ob_get_contents();
ob_end_clean();
$file_path= "./somefile.xml";
$file_handle=fopen($somefile,'w');
fwrite($file_handle,utf8_encode($trapped_content));
}
?>
Some side notes:
- note that the utf8_encode function goes inside the fwrite() function.
- when troubleshooting, make sure to transfer text file (xml included) and scripts in ascii mode
when using ftp. For some unknown reason my ftp client did not have xml set as an ascii transfer
candidate and was automatically tranfering them in binary. That little "feature" ended up
costing me hours of frustration, as the encoding information would just "vanish" between
transfer and I kept scratching my head as to why manually created utf8 files were not behaving as
they should.