How to put string in array, split by new line?

匿名 (未验证) 提交于 2019-12-03 02:11:02

问题:

I have a string with line breaks in my database. 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 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)



回答2:

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!



回答3:

I've always used this with great success:

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

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



回答4:

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

explode(PHP_EOL,$string) 


回答5:

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

$arrayOfLines = explode("\n",                     str_replace(array("\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



回答6:

David: Great direction, but you missed \r. this worked for me:

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


回答7:

You don't need preg_* functions nor preg patterns nor str_replace within, etc .. in order to sucessfuly break a string into array by newlines. In all scenarios, be it Linux/Mac or m$, 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.



回答8:

explode("\n", $str); 

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



回答9:

<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); 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!