Define a newline return method in PHP

What would be the best way to discover the newline return method in PHP. CR, LF or CR + LF

And, if possible, convert it to the specified type.

+3
source share
4 answers
define('NL_NIX', "\n");
define('NL_WIN', "\r\n");
define('NL_MAC', "\r");


function newline_type($string)
{
    if (strpos($string, NL_WIN) !== false) {
        return NL_WIN;
    } elseif(strpos($string, NL_MAC) !== false) {
        return NL_MAC;
    } elseif(strpos($string, NL_NIX) !== false) {
        return NL_NIX;
    }
}

Checks which row of a row is in a row. 0 is returned when a new line is not found.

To normalize / standardize all newlines, use the following function:

function newline_convert($string, $newline)
{
    return str_replace(array(NL_WIN, NL_MAC, NL_NIX), $newline, $string);
}

Hope this helps!

+6
source

This will check the line for the newline character (CR or LF):

function has_newline($string)
{
    return (strpos($string, "\r") !== false || strpos($string, "\n") !== false);
}

( \rmeans CR and \nmeans LF)

. , CR LF :

function replace_returns($string)
{
    return str_replace("\r", "\n", $string);
}
+1

PHP_EOL .

+1

You can do $ string = nl2br ($ string) so that your line break is changed to

<br />. 

Then you can manipulate the string, for example. split it into first appearance

<br />

like this:

list($first, $second) = explode('<br />', $string, 2);
0
source

Source: https://habr.com/ru/post/1721265/


All Articles