views:

1150

answers:

2

For example, when I create a new file:

$message = "Hello!";    
$fh = fopen(index.html, 'w');
fwrite($fh, $message);
fclose($fh);

How can I set it's encoding(utf-8 or shift-jis or euc-jp) and linebreaks(LF or CR+LF or CR) in PHP?

A: 

To add carriage returns and linefeeds use the special characters \r and \n. So:

$message = "Hello!\r\n";
Vex
+3  A: 

The encoding of a string literal should match the encoding of the source file, to convert between encodings you could use iconv.

$utf8=iconv("ISO-8859-1", "UTF-8", $message);

Line breaks are entirely up to you. You could use the PHP_EOL constant, or if you think you might need to vary the type of line break, store the desired sequence in a variable and configure it at runtime.

Paul Dixon