Help us understand the problem. What is going on with this article?

PHPで改行コードを統一する関数: CRLF, CR, LF が混在してる文字列を LF に変換するなど

More than 5 years have passed since last update.

各OS向けにCSVを出力するときとか。

<?php

function convertEOL($string, $to = "\n")
{   
    return preg_replace("/\r\n|\r|\n/", $to, $string);
}

$string = "CR + LF: \r\n CR: \r LF: \n";

echo "Testing string:", PHP_EOL;
echo json_encode($string), PHP_EOL;

echo "For MacOSX, Unix, Linux", PHP_EOL;
echo json_encode(convertEOL($string)), PHP_EOL;

echo "For Windows", PHP_EOL;
echo json_encode(convertEOL($string, "\r\n")), PHP_EOL;

echo "For MacOS 9", PHP_EOL;
echo json_encode(convertEOL($string, "\r")), PHP_EOL;
結果
Testing string:
"CR + LF: \r\n CR: \r LF: \n"
For MacOSX, Unix, Linux
"CR + LF: \n CR: \n LF: \n"
For Windows
"CR + LF: \r\n CR: \r\n LF: \r\n"
For MacOS 9
"CR + LF: \r CR: \r LF: \r"
suin
Qiita 4位/TypeScript入門書執筆中/TypeScripterのための座談会「YYTypeScript」主催/『実践ドメイン駆動設計』書籍邦訳レビュア/分報Slack考案/YYPHP主催/CodeIQマガジン執筆/株式会社クラフトマンソフトウェア創設/Web自動テスト「ShouldBee」の開発/TypeScript/DDD/OOP
https://yyts.connpass.com/
craftsman_software
「インフラの心配は、もうおしまい」 インフラ運用を自動化し、手作業を限りなくゼロにする会社
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away