write file with special characters in php
<p>SOLUTION:</p>

<pre><code>    $output = '–– € ––'; 
//written like this php 5 does not understand because it interprets it as single-byte chars.
//so i found the function below to write a multi-byte char in a string.

//unicode version of php's chr()
function uchr ($codes) {
    if (is_scalar($codes)) $codes= func_get_args();
    $str= '';
    foreach ($codes as $code) $str.= html_entity_decode('&amp;#'.$code.';',ENT_NOQUOTES,'UTF-8');
    return $str;
}

//decimal values of unicode chars: – 8211 - 8211, [space] 32, € 8364,[space] 32, – 8211 - 8211
$output = uchr(8211,8211,32,8364,32,8211,8211);

//or
$output = uchr(8211,8211).' '.uchr(8364).' '.uchr(8211,8211);

echo $output;
</code></pre>

<p>QUESTION:</p>

<p>How can i write these special chars to a simple file?</p>

<pre><code>$file = "./upload/myfile.txt";
$output = "–– € ––".PHP_EOL; // the "–" is not an underscore _ or - but –
file_put_contents($file, $output);
</code></pre>

<p>If I access this file from the browser <a href="http://mydomain.com/upload/myfile.txt" rel="nofollow">http://mydomain.com/upload/myfile.txt</a> i only get "�" characters.</p>

<p>However if i save "–– € ––" with Zend Developer or my local texteditor (on OSX) and upload this everything is perfectly fine. The browser shows it correctly.</p>

<p>How can i achieve this with php? it seems php uses a different way of writing the file than my macbook. thought i thought php's standard was UTF-8 and i also saved the file as UTF-8 in my local text editor.</p>

<p>EXTRA INFO: in the .htaccess file that's in the upload folder i wrote:</p>

<pre><code>  AddDefaultCharset utf-8
  AddCharset utf-8 .txt
</code></pre>

<p>otherwise the firebug addon from firefox gave a message that the charset was not specified.</p>

<p>any ideas?
It has to do with saving the file because my uploaded file shows correctly.</p>

<p>i tried different options while saving the file like:</p>

<pre><code>$output = mb_convert_encoding($output, 'UTF-8', 'OLD-ENCODING');
</code></pre>

<p>and the iconv function of php, but i cant find the solution.</p>

<p>any help is greatly appreciated.</p>

<p>EDIT: if i get the content from my uploaded file and echo it the following happens</p>

<pre><code>$output = file_get_contents('./upload/myuploadedfile.txt',FILE_USE_INCLUDE_PATH); 
//it show correctly –– € ––
$output = $output[1]; //it shows a �
$output = $output[3]; //it shows a �

echo $output;
</code></pre>
