Split a string into multiple lines by newlines.
P粉014293738
2023-08-06 16:02:16
<p>I have a string with newlines. I want to convert this string into an array and for each newline character, skip one index position in the array. <br /><br />If the string is: </p><p><br /></p>
<pre class="brush:php;toolbar:false;">My text1
My text2
My text3</pre>
<p>The result I want is this:</p>
<pre class="brush:php;toolbar:false;">Array
(
[0] => My text1
[1] => My text2
[2] => My text3
)</pre>
<p><br /></p>
I have been using this with great success:
With the last \r, thanks @LobsterMan):
You can use the explode function and use "\n" as the separator:
For example, if you have the following code snippet:
You will get the following output:
Note that you have to use a Note that you must use a double-quoted string so that \n will be interpreted as a newline character. (See the man page for more details.)