282

I have a string with new line characters. I want to convert that string into an array, and for every new line, jump one index place in the array.

If the string is:

My text1
My text2
My text3

The result I want is this:

Array
(
    [0] => My text1
    [1] => My text2
    [2] => My text3
)
1
  • You should normalize the newlines first, probably. The method s($yourString)->normalizeLineEndings() is available with github.com/delight-im/PHP-Str (library under MIT License) which has lots of other useful string helpers. You may want to take a look at the source code.
    – caw
    Jul 25, 2016 at 3:23

19 Answers 19

442

I've always used this with great success:

$array = preg_split("/\r\n|\n|\r/", $string);

(updated with the final \r, thanks @LobsterMan)

5
  • 1
    This is the answer. The validated one is WRONG. Well, hesselbom has it too... You could also use this equivalent: preg_split('/\n|\r/', $string, -1, PREG_SPLIT_NO_EMPTY); for the sake of beauty :) Why is this the only good answer? Because you cannot assume what type of end of line you will get: Mac (\r), Windows (\r\n), or Unix (\n).
    – Ninj
    Jan 8, 2015 at 23:08
  • 17
    \R matches \n, \r and \r\n
    – mpen
    Oct 13, 2016 at 17:44
  • 2
    Or shorter /\r?\n/ Oct 23, 2019 at 16:59
  • 1
    This example is correct because you can't just take any split operation that is just based on a single character. If you would do that, triggering on '\r' or '\n' you will end up with a superfluous empty line in case of a Windows "\r\n" ending. And it's important that the two-character Windows separator is tested first. Feb 14, 2020 at 10:56
  • what about <br /> Oct 21, 2022 at 11:32
336

You can use the explode function, using "\n" as separator:

$your_array = explode("\n", $your_string_from_db);

For instance, if you have this piece of code:

$str = "My text1\nMy text2\nMy text3";
$arr = explode("\n", $str);
var_dump($arr);

You'd get this output:

array
  0 => string 'My text1' (length=8)
  1 => string 'My text2' (length=8)
  2 => string 'My text3' (length=8)


Note that you have to use a double-quoted string, so \n is actually interpreted as a line-break.
(See that manual page for more details.)

8
  • 61
    Instead of \n you can use the predefined constant PHP_EOL.
    – Tim
    Jul 19, 2012 at 14:58
  • 47
    Everyone please be careful with this solution, as it does not work on all newlines. I've had the most success with David's answer
    – Maurice
    Aug 29, 2012 at 7:31
  • 2
    You must split at either \n or\r to be able to handle all kind of texts - this will only work with linux & windows newlines. Mac newlines will be disregarded! (\r) Jun 2, 2016 at 1:30
  • 1
    I guess Tims answer/comment is not right, because this will only match the linebreak an YOUR system, but when you get strings that has linebreaks from other systems it wont work! Had this problem with emails
    – Asara
    Nov 30, 2016 at 12:20
  • 10
    Nope this answer and the comments on this answer are WRONG! Because this does not factor in the OS newline character, especially not PHP_EOL. You must use preg_split("/\\r\\n|\\r|\\n/", $value).
    – kjdion84
    Sep 18, 2017 at 15:24
293

A line break is defined differently on different platforms, \r\n, \r or \n.

Using RegExp to split the string you can match all three with \R

So for your problem:

$array = preg_split ('/$\R?^/m', $string);

That would match line breaks on Windows, Mac and Linux!

11
  • 3
    I used this as well instead of the accepted answer and any other answers on this thread. Just leaving this comment as an informative. Jun 1, 2012 at 12:55
  • 3
    Actually, this did not work for me. Sometimes newlines where still present in the array keys.
    – Maurice
    Aug 29, 2012 at 7:29
  • 2
  • 37
    Just use $array = preg_split ('/\R/', $string); Feb 15, 2013 at 3:42
  • 19
    Not working sometimes! preg_split("/\r\n|\n|\r/", $string) better choice
    – Alexey B.
    Aug 28, 2013 at 10:26
39

PHP already knows the current system's newline character(s). Just use the EOL constant.

explode(PHP_EOL,$string)
3
  • 21
    Yes, but one can edit a file or as in this example a db entry in Windows then use it on a Linux system, for example. I think a general approach would suit better.
    – Cranio
    Dec 7, 2012 at 11:03
  • 2
    I'm not sure this is right. If a text area on a web page gets submitted, it could have different end-of-line characters depending on the user's browser, not the server's operating system. So no matter what OS you are using, you will need to be able to parse anything. This is assuming you're doing web stuff of course.
    – Magmatic
    Nov 8, 2016 at 20:52
  • 1
    You might then have to use trim() on the resulting strings to remove any extraneous whitespace.
    – Tim
    Dec 7, 2016 at 15:12
38

An alternative to Davids answer which is faster (way faster) is to use str_replace and explode.

$arrayOfLines = explode("\n",
                    str_replace(["\r\n","\n\r","\r"],"\n",$str)
            );

What's happening is:
Since line breaks can come in different forms, I str_replace \r\n, \n\r, and \r with \n instead (and original \n are preserved).
Then explode on \n and you have all the lines in an array.

I did a benchmark on the src of this page and split the lines 1000 times in a for loop and:
preg_replace took an avg of 11 seconds
str_replace & explode took an avg of about 1 second

More detail and bencmark info on my forum

4
  • Your forum link (and the entire domain) seem to be down
    – Nikita 웃
    Jul 29, 2018 at 7:58
  • \n\r ...Which operating system uses the newline sequence in reverse like this? Dec 20, 2020 at 20:51
  • @mickmackusa, none that I know of. It was so long ago, I'm not sure why I did it that way. Thinking I should remove the \n\r?
    – Reed
    Dec 20, 2020 at 21:24
  • 1
    As far as I know, you only need to retain \r\n in the search array (and for that matter, it doesn't need to be an array anymore). I am interested in the claim that preg_ was 11 times slower. You did not include the pattern that you used. Your above snippet makes 4 passes over the input. A good preg_ technique will make only one pass over the input. Regex is not known for its speed, but I think your claim requires substantiation. Please post your benchmark details if you are going to keep this performance claim in your answer. Dec 20, 2020 at 22:05
22

David has a great direction, but it missed \r. This worked for me:

$array = preg_split("/(\r\n|\n|\r)/", $string);
1
  • The capture group is not necessary. Dec 20, 2020 at 20:59
16

You don't need preg_* functions, preg patterns, str_replace within, etc. in order to successfully break a string into array by newlines. In all scenarios, be it Linux, Mac, or Windows, this will do.

<?php
    $array = explode(PHP_EOL, $string);
    // ...
    $string = implode(PHP_EOL, $array);
?>

PHP_EOL is a constant holding the line break character(s) used by the server platform.

6
  • 7
    the file could come from another system with different new lines, particularry in a net environment for wich is used PHP
    – Luca C.
    Jul 25, 2016 at 8:54
  • Which is exactly the reason this constant exists.. and handles that particular problem.
    – Spooky
    Jul 28, 2016 at 15:14
  • if you take a file from windows it does not match the same as a file from unix, or mac, right?
    – Luca C.
    Jul 28, 2016 at 15:53
  • If You are using pure utf-8 all the time, everywhere, utf8 files included, normally, and You have nothing else but PHP_EOL inside code of Yours for line break detections, it will be matched as described, and no unpredicted behavior will occur. Keep in mind that it's not only me yelling and claiming this. PHP_EOL usability is quite confirmed.
    – Spooky
    Jul 28, 2016 at 16:04
  • 1
    In Your case, if sources come from somewhere else and aren't formed well, whatever, it's maybe better if You use str_replace (faster than regexp). ... all in all, be it regexp. or str_replace or PHP_EOL, there's one good old sentence saying: "If it works - don't touch it!". :)
    – Spooky
    Jul 29, 2016 at 8:18
15

Use: $array = preg_split('/\s*\R\s*/', trim($text), NULL, PREG_SPLIT_NO_EMPTY);

This worked best for me because it also eliminates leading (second \s*) and trailing (first \s*) whitespace automatically and also skips blank lines (the PREG_SPLIT_NO_EMPTY flag).

Options

If you want to keep leading whitespace, simply get rid of the second \s* and make it an rtrim() instead...

$array = preg_split('/\s*\R/', rtrim($text), NULL, PREG_SPLIT_NO_EMPTY);

If you need to keep empty lines, get rid of the NULL (it is only a placeholder) and PREG_SPLIT_NO_EMPTY flag, like so...

$array = preg_split('/\s*\R\s*/', trim($text));

Or keeping both leading whitespace and empty lines...

$array = preg_split('/\s*\R/', rtrim($text));

I don't see any reason why you'd ever want to keep trailing whitespace, so I suggest leaving the first \s* in there. But, if all you want is to split by new line (as the title suggests), it is this simple (as mentioned by Jan Goyvaerts)...

$array = preg_split('/\R/', $text);
0
11

There is quite a mix of direct and indirect answers on this page and some good advice in comments, but there isn't an answer that represents what I would write in my own project.

PHP Escape Sequence \R documentation: https://www.php.net/manual/en/regexp.reference.escape.php#:~:text=line%20break,\r\n

Code: (Demo)

$string = '
My text1

My text2


My text3


';

var_export(
    preg_split('/\R+/', $string, 0, PREG_SPLIT_NO_EMPTY)
);

Output:

array (
  0 => 'My text1',
  1 => 'My text2',
  2 => 'My text3',
)

The OP makes no mention of trimming horizontal whitespace characters from the lines, so there is no expectation of removing \s or \h while exploding on variable (system agnostic) new lines.

While PHP_EOL is sensible advice, it lacks the flexibility appropriately explode the string when the newline sequence is coming from another operating system.

Using a non-regex explode will tend to be less direct because it will require string preparations. Furthermore, there may be mopping up after the the explosions if there are unwanted blank lines to remove.

Using \R+ (one or more consecutive newline sequences) and the PREG_SPLIT_NO_EMPTY function flag will deliver a gap-less, indexed array in a single, concise function call. Some people have a bias against regular expressions, but this is a perfect case for why regex should be used. If performance is a concern for valid reasons (e.g. you are processing hundreds of thousands of points of data), then go ahead and invest in benchmarking and micro-optimization. Beyond that, just use this one-line of code so that your code is brief, robust, and direct.

9
explode("\n", $str);

The " (instead of ') is quite important as otherwise, the line break wouln't get interpreted.

7
<anti-answer>

As other answers have specified, be sure to use explode rather than split because as of PHP 5.3.0 split is deprecated. i.e. the following is NOT the way you want to do it:

$your_array = split(chr(10), $your_string);

LF = "\n" = chr(10), CR = "\r" = chr(13)

</anti-answer>
4

Use:

$str = "My text1\nMy text2\nMy text3";
$arr = explode("\n", $str);

foreach ($arr as $line_num => $line) {
    echo "Line #<b>{$line_num}</b>: " . htmlspecialchars($line) . "<br />\n";
}

True array:

$str = "My text1\nMy text2\nMy text3";
$arr = explode("\n", $str);

$array = array();

foreach ($arr as $line) { // loop line by line and convert into array
    $array[] = $line;
};

print_r($array); // display all value

echo $array[1]; // display index 1

Embed online:

body, html, iframe {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
<iframe src="https://ideone.com/vE1gst" ></iframe>

2
  • 1
    Add some explanation with answer for how this answer help OP in fixing current issue Jun 21, 2017 at 21:48
  • What is "inisiasi"? Feb 2, 2022 at 2:35
3

For anyone trying to display cronjobs in a crontab and getting frustrated on how to separate each line, use explode:

$output = shell_exec('crontab -l');
$cron_array = explode(chr(10),$output);

using '\n' doesnt seem to work but chr(10) works nicely :D

hope this saves some one some headaches.

2
  • Hi Jeff, welcome to SO. You should rewrite this as a question and answer, rather than an answer on somebody else's question. This is a Q&A site, not a general forum. Have a look at the FAQ - stackoverflow.com/faq Jul 13, 2011 at 7:35
  • Using '\n' doesn't work because escape sequences are not interpreted in single quotes, you need to use double quotes instead: "\n" instead of 'n' Read php.net/manual/en/language.types.string.php Sep 8, 2017 at 12:50
3

That's my way:

$lines = preg_split('/[\r\n]+/', $db_text, NULL, PREG_SPLIT_NO_EMPTY);

This also will skip all empty lines too.

3

You can use this:

 str_getcsv($str, PHP_EOL);
1
2

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

<br />. 

This way it does not matter if the system uses \r\n or \n or \r

Then you can feed it into an array:

$array = explode("<br />", $string);
2
  • 1
    Note: nl2br does not replace the line breaks... it only inserts a break tag before each line break. So all of your array items other than the first will begin with a line break. If that doesn't matter to you, then this method is the fastest. If that does matter, Jakar's answer is the fastest.
    – Aust
    Oct 20, 2015 at 20:56
  • The performance benefit of a non-regex technique may be true, but Reed's benchmark details are not currently included. There may be untruths in the details/results. Dec 20, 2020 at 22:10
2

I picked this up in the PHP documentation:

<?php
  // Split the phrase by any number of commas or space characters,
  // which include " ", \r, \t, \n and \f

  $keywords = preg_split("/[\s,]+/", "hypertext language, programming");
  print_r($keywords);
?>
1
  • The answer would definitely mangle the OP's text. This is the correct answer to a different question; in other words, this is provably incorrect for this page. Dec 20, 2020 at 21:59
0

Using only the 'base' package is also a solution for simple cases:

> s <- "a\nb\rc\r\nd"
> l <- strsplit(s,"\r\n|\n|\r")
> l  # the whole list...
[[1]]
[1] "a" "b" "c" "d"
> l[[1]][1] # ... or individual elements
[1] "a"
> l[[1]][2]
[1] "b"
> fun <- function(x) c('Line content:', x) # handle as you wish
> lapply(unlist(l), fun)
1
  • What is this? Some interactive session with the PHP REPL? Or something else? Feb 2, 2022 at 2:38
-1

This method always works for me:

$uniquepattern = "gd$#%@&~#" // Any set of characters which you don’t expect to be present in user input $_POST['text']. Better use at least 32 characters.
$textarray = explode($uniquepattern, str_replace("\r", "", str_replace("\n", $uniquepattern, $_POST['text'])));
1
  • 2
    Love the swearing as part of the code. :) Anyway, replacing with \n is simpler as well as safer (look at the other answers). Mar 11, 2015 at 12:50

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.