Search code examples
phpencodingendiannessucs2

Output file in PHP encoded as OCS-2 Big Endian


How can I write a file in PHP that has the encoding OCS-2 Big Endian? An industrial system I am trying to integrate with requires this encoding format.

To simplify the problem, supposing the content I was trying to write to a file was stored in the variable $sXML, in this case XML content with encoding set in the XML header ready:

$sXML = '<?xml version="1.0" encoding="utf-16BE"?>';
$sXML .= '<envelope><node>1</node><node>2</node><node>3</node></envelope>';
/* Insert solution here */
file_put_contents( 'filename.xml', $sXML );

So far I have tried such as:

  1. $sXML = iconv( '', 'UCS-2BE', $sXML );
  2. $sXML = html_entity_decode( htmlentities( $sXML, ENT_QUOTES, 'Windows-1252' ), ENT_QUOTES, 'UCS-2BE' );
  3. $sXML = mb_convert_encoding( $sXML, 'UCS-2BE', 'HTML-ENTITIES' );

And while all of these ruin the human readability of the file generated in Notepad++, none of them cause the file encoding to be set to UCS-2BE.

If you're not familiar with OCS-2 Big Endian encoding, this might prove to be essential/helpful reading: Better Explained (blog): Understanding Big and Little Endian Byte Order.

I've tried looking through the other questions here on StackOverflow but found a solution to this specific problem, or managed to adapt any of the others to this. Any help would be really appreciated! Thank you.


Solution

  • As it turns out I was very close, and eventually I worked it out. The verified working solution is as follows:

    $sXML = pack( 'n', 0xFEFF ) . iconv( '', 'UCS-2BE', $sXML );
    

    Thank you all for your help troubleshooting this problem.