src/Formatter/SerializeFormatter.php
protected function preserveLines($data, bool $reverse)
    {
        $search = ["\n", "\r"];
        $replace = ['\\n', '\\r'];

        if ($reverse) {
            $search = ['\\n', '\\r'];
            $replace = ["\n", "\r"];
        }

        if (is_string($data)) {
            $data = str_replace($search, $replace, $data);
        } elseif (is_array($data)) {
            foreach ($data as &$value) {
                $value = $this->preserveLines($value, $reverse);
            }
            unset($value);
        }

        return $data;
    }
protected function preserveLines($data, bool $reverse)
    {
        $search = ["\n", "\r"];
        $replace = ['\\n', '\\r'];

        if ($reverse) {
            $search = ['\\n', '\\r'];
            $replace = ["\n", "\r"];
        }

        if (is_string($data)) {
            $data = str_replace($search, $replace, $data);
        } elseif (is_array($data)) {
            foreach ($data as &$value) {
                $value = $this->preserveLines($value, $reverse);
            }
            unset($value);
        }

        return $data;
    }
Preserve new lines, recursive function.

@param mixed $data
@param bool $reverse

@return mixed