Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About PHP XMLwriter() encoding input and output

I've been looking everywhere for a detailed explanation on how the XMLWriter() encodes its output but couldn't find it. I would like to know what encoding should the input data be in if I want an specific output encoding, for example ISO-8859-1. Should I give it the input data in the same format?

For example here:

$xw->writeElement('garantie','Garantie à vie'); *edited
$xw->endElement();

Should I do any encoding conversion on the string 'Garantie à vie' or does the XMLWriter() convert it automatically? Should the string be in ISO-8859-1 or UTF-8?

like image 934
Pato Avatar asked Mar 24 '26 14:03

Pato


1 Answers

Should I do any encoding conversion on the string 'Garantie à vie' or does the XMLWriter() convert it automatically?

XMLWriter accepts UTF-8 string input in PHP and it will automatically re-encode it into the output encoding (if needed). This internal re-encoding is not always needed because an XML's default default encoding is UTF-8 already.

Should the string be in ISO-8859-1 or UTF-8?

The string should be UTF-8 encoded.

Example (with an UTF-8 encoded string; Demo):

<?php
/**
 * About PHP XMLwriter() encoding input and output
 *
 * @link https://stackoverflow.com/a/19046825/367456
 * @link https://eval.in/51120
 */

$xmlWriter = new XMLWriter();
$xmlWriter->openMemory();
$xmlWriter->startDocument('1.0', 'US-ASCII');
$xmlWriter->writeElement('garantie', 'Garantie à vie');
$xmlWriter->endDocument();
echo $xmlWriter->flush();

Output:

<?xml version="1.0" encoding="US-ASCII"?>
<garantie>Garantie &#224; vie</garantie>

See as well:

  • A more detailed explanation of XMLWriter character set encodings can be found in an earlier answer which was answering the "php XML export issue with XMLWriter using writeAttribute() method" question. It also explains how to change the encoding of the output document.
  • The question How to check if string is a valid XML element name? covers the topic of valid element names in PHP (UTF-8 encoded and other) quite well.
like image 195
hakre Avatar answered Mar 27 '26 05:03

hakre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!