<textarea> の値の改行コード

<textarea> の値の改行コードは、状況によって異なります。

目次

JavaScript 上での改行コード

JavaScript 上の value (HTMLTextAreaElement の value プロパティ) で取得できる値の改行コードは LF (U+000A ラインフィード。\n) です。
別の改行コードがあっても LF に正規化されます。


ここに結果が表示されます。

フォーム送信された値の改行コード

フォーム送信された値の改行コードは CRLF (U+000D キャリッジリターン / U+000A ラインフィード。\r\n) です。
別の改行コードがあっても CRLF に正規化されます。

例 (PHP)

ソースコード
<?php
$ta1 = '';
if (isset($_POST['ta1'])) {
    // 改行コードを \r, \n に変換
    $ta1 = strtr($_POST['ta1'], ["\r" => "\\r", "\n" => "\\n"]);
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>テスト</title>
</head>
<body>
    <form method="post">
        <textarea name="ta1">1行目
2行目</textarea><br>
        <button type="submit">改行コード表示</button>
    </form>
    <pre><?php echo $ta1 ?></pre>
</body>
</html>
結果 (画像のため操作はできません)

参考

※HTML Living Standard — Last Updated 23 August 2023

For historical reasons, the element's value is normalized in three different ways for three different purposes. The raw value is the value as it was originally set. It is not normalized. The API value is the value used in the value IDL attribute, textLength IDL attribute, and by the maxlength and minlength content attributes. It is normalized so that line breaks use U+000A LINE FEED (LF) characters. Finally, there is the value, as used in form submission and other processing models in this specification. It is normalized as for the API value, and in addition, if necessary given the element's wrap attribute, additional line breaks are inserted to wrap the text at the given width.

4.10.11 The textarea element

The application/x-www-form-urlencoded and text/plain encoding algorithms take a list of name-value pairs, where the values must be strings, rather than an entry list where the value can be a File. The following algorithm performs the conversion.

To convert to a list of name-value pairs an entry list entry list, run these steps:

  1. Let list be an empty list of name-value pairs.
  2. For each entry of entry list:
    1. Let name be entry's name, with every occurrence of U+000D (CR) not followed by U+000A (LF), and every occurrence of U+000A (LF) not preceded by U+000D (CR), replaced by a string consisting of U+000D (CR) and U+000A (LF).
    2. If entry's value is a File object, then let value be entry's value's name. Otherwise, let value be entry's value.
    3. Replace every occurrence of U+000D (CR) not followed by U+000A (LF), and every occurrence of U+000A (LF) not preceded by U+000D (CR), in value, by a string consisting of U+000D (CR) and U+000A (LF).
    4. Append to list a new name-value pair whose name is name and whose value is value.
  3. Return list.

4.10.21.6 Converting an entry list to a list of name-value pairs

The multipart/form-data encoding algorithm, given an entry list entry list and an encoding encoding, is as follows:
  1. For each entry of entry list:
    1. Replace every occurrence of U+000D (CR) not followed by U+000A (LF), and every occurrence of U+000A (LF) not preceded by U+000D (CR), in entry's name, by a string consisting of a U+000D (CR) and U+000A (LF).
    2. If entry's value is not a File object, then replace every occurrence of U+000D (CR) not followed by U+000A (LF), and every occurrence of U+000A (LF) not preceded by U+000D (CR), in entry's value, by a string consisting of a U+000D (CR) and U+000A (LF).

4.10.21.8 Multipart form data