Skip to content

The flag 'JSON_UNESCAPED_LINE_TERMINATORS' seems to be useless #21947

@JoeHorn

Description

@JoeHorn

Description

The following code:

<?php
$strs = [
  'str1' => "Line1\nLine2",
  'str2' => "Line1\r\nLine2",
  'str3' => 'Line1' . PHP_EOL . 'Line2',
];
foreach ($strs as $k => $v) {
  echo "** {$k} **" . PHP_EOL . $v . PHP_EOL . PHP_EOL;
}

echo '** json_encoded **' . PHP_EOL . json_encode(
  $strs,
  JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS
) . PHP_EOL . PHP_EOL;

Resulted in this output:

** str1 **
Line1
Line2

** str2 **
Line1
Line2

** str3 **
Line1
Line2

** json_encoded **
{"str1":"Line1\nLine2","str2":"Line1\r\nLine2","str3":"Line1\nLine2"}

But I expected this output instead:

** str1 **
Line1
Line2

** str2 **
Line1
Line2

** str3 **
Line1
Line2

** json_encoded **
{"str1":"Line1
Line2","str2":"Line1
Line2","str3":"Line1
Line2"}

PHP Version

PHP 8.3.6 (cli) (built: Mar 20 2026 02:32:55) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies

Operating System

Ubuntu 24.04

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions