Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In order to have a valid final XML text, you need to escape all XML entities and have the text written in the same encoding as the XML document processing-instruction states it (the "encoding" in the <code>&lt;?xml</code> line). The accented characters don't need to be escaped as long as they are encoded as the document.</p> <p>However, in many situations simply escaping the input with <code>htmlspecialchars</code> may lead to double-encoded entities (for example <code>&amp;eacute;</code> would become <code>&amp;amp;eacute;</code>), so I suggest decoding html entities first:</p> <pre><code>function xml_escape($s) { $s = html_entity_decode($s, ENT_QUOTES, 'UTF-8'); $s = htmlspecialchars($s, ENT_QUOTES, 'UTF-8', false); return $s; } </code></pre> <p>Now you need to make sure all accented characters are valid in the XML document encoding. I strongly encourage to always encode XML output in UTF-8, since not all the XML parsers respect the XML document processing-instruction encoding. If your input might come from a different charset, try using <code>utf8_encode()</code>.</p> <p>There's a special case, which is your input may come from one of these encodings: ISO-8859-1, ISO-8859-15, UTF-8, cp866, cp1251, cp1252, and KOI8-R -- PHP treats them all the same, but there are some slight differences in them -- some of which even <code>iconv()</code> cannot handle. I could only solve this encoding issue by complementing <code>utf8_encode()</code> behavior:</p> <pre><code>function encode_utf8($s) { $cp1252_map = array( "\xc2\x80" =&gt; "\xe2\x82\xac", "\xc2\x82" =&gt; "\xe2\x80\x9a", "\xc2\x83" =&gt; "\xc6\x92", "\xc2\x84" =&gt; "\xe2\x80\x9e", "\xc2\x85" =&gt; "\xe2\x80\xa6", "\xc2\x86" =&gt; "\xe2\x80\xa0", "\xc2\x87" =&gt; "\xe2\x80\xa1", "\xc2\x88" =&gt; "\xcb\x86", "\xc2\x89" =&gt; "\xe2\x80\xb0", "\xc2\x8a" =&gt; "\xc5\xa0", "\xc2\x8b" =&gt; "\xe2\x80\xb9", "\xc2\x8c" =&gt; "\xc5\x92", "\xc2\x8e" =&gt; "\xc5\xbd", "\xc2\x91" =&gt; "\xe2\x80\x98", "\xc2\x92" =&gt; "\xe2\x80\x99", "\xc2\x93" =&gt; "\xe2\x80\x9c", "\xc2\x94" =&gt; "\xe2\x80\x9d", "\xc2\x95" =&gt; "\xe2\x80\xa2", "\xc2\x96" =&gt; "\xe2\x80\x93", "\xc2\x97" =&gt; "\xe2\x80\x94", "\xc2\x98" =&gt; "\xcb\x9c", "\xc2\x99" =&gt; "\xe2\x84\xa2", "\xc2\x9a" =&gt; "\xc5\xa1", "\xc2\x9b" =&gt; "\xe2\x80\xba", "\xc2\x9c" =&gt; "\xc5\x93", "\xc2\x9e" =&gt; "\xc5\xbe", "\xc2\x9f" =&gt; "\xc5\xb8" ); $s=strtr(utf8_encode($s), $cp1252_map); return $s; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload