vladaruz
vladaruz

Reputation: 229

Setting encoding, newline, linebreaks, end-of-line (EOL) in PHP

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?

Upvotes: 1

Views: 7997

Answers (2)

Paul Dixon
Paul Dixon

Reputation: 300825

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.

Upvotes: 4

Vex
Vex

Reputation: 1497

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

$message = "Hello!\r\n";

Upvotes: 0

Related Questions