Explode PHP string by new line
Best Practice
As mentioned in the comment to the first answer, the best practice is to use the PHP constant PHP_EOL which represents the current system's EOL (End Of Line).
$skuList = explode(PHP_EOL, $_POST['skuList']);
PHP provides a lot of other very useful constants that you can use to make your code system independent, see this link to find useful and system independent directory constants.
Warning
These constants make your page system independent, but you might run into problems when moving from one system to another when you use the constants with data stored on another system. The new system's constants might be different from the previous system's and the stored data might not work anymore. So completely parse your data before storing it to remove any system dependent parts.
UPDATE
Andreas' comment made me realize that the 'Best Practice' solution I present here does not apply to the described use-case: the server's EOL (PHP) does not have anything to do with the EOL the browser (any OS) is using, but that (the browser) is where the string is coming from.
So please use the solution from @Alin_Purcaru to cover all your bases:
$skuList = preg_split('/\r\n|\r|\n/', $_POST['skuList']);
Explode a string with line break
The problem is that \r\n
in the original text are NOT end of line symbols - it's just literally 'backslash-r-backslash-n' sequence. Hence you need to take this into account:
$r = explode('\r\n', $roster[0]);
... i.e., use single quotes to delimit the string.
PHP divide string by new line(\n) character
Just use the explode function and use the newline character:
$array=explode("\n",$string);
print_r($array);
Having said that, different OS will use different new line breaks. Some will use \n
while others will use \r\n
which you might want to look into.
You can combine what you are doing with the nl2br function which covers all the options if you really want to - though I would consider it potentially overkill/complicating the issue:
$array=explode("<br>",nl2br($string,false));
print_r($array);
explode string at newline,space,newline,space in PHP
Just use explode()
with PHP_EOL." ".PHP_EOL." "
, which is of the format "newline, space, newline, space". Using PHP_EOL
, you get the correct newline-format for your system.
$split = explode(PHP_EOL." ".PHP_EOL." ", $string);
print_r($split);
Live demo at https://3v4l.org/WpYrJ
How to explode a string, with spaces and new lines?
You could just do a
$segments = preg_split('/[\s]+/', $string);
This function will split $string
at every whitespace (\s
) occurrence, including spaces, tabs and newlines. Multiple sequential white spaces will count as one (e.g. "hello, \n\t \n\nworld!"
will be split in hello,
and world!
only, without empty strings in the middle.
See function reference here.
Explode by comma and new line together
Try with this:
$split_strings = preg_split('/[\ \n\,]+/', $your_string);
Split string by new line characters
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.)
PHP explode line break and strip blank lines and white spaces
Try this...
Input
$data = " Object One
Object Two
Object Three
Object Four
Object Five
Object Six";
Solution
$result = array_filter(array_map('trim',explode("\n", $data)));
echo "<pre>"; print_r($result);
Output
Array
(
[0] => Object One
[3] => Object Two
[6] => Object Three
[8] => Object Four
[9] => Object Five
[11] => Object Six
)
Split string to new line
Normally, I would say that that's the wordwrap
function is for:
$str = "test string more and more text very long string can be upto length of 90";
$wrapped = wordwrap($str, 30, "\n", true);
list($line1, $line2, $line3) = explode("\n", $wrapped . "\n\n");
(The extra \n
s on $wrapped
are to prevent errors in case fewer than 3 lines are made.)
However, your problem is a bit different. wordwrap
will sometimes make a 4th line rather than cut a word in half, which is what you need to do. wordwrap
will only ever make a 4th line if cutting a word is required, so perhaps try this:
$str = "test string more and more terxt very long string can be upto length of 90 bla bla bla3rr33";
$maxLen = 30;
$wrapped = wordwrap($str, $maxLen, "\n", true);
list($line1, $line2, $line3, $line4) = explode("\n", $wrapped . "\n\n\n");
if ($line4 !== '') {
//fallback case: we have to split words in order to fit the string properly in 3 lines
list($line1, $line2, $line3) = array_map('trim', str_split($str, $maxLen));
}
There is one bad thing that this code can do: it will sometimes split two words when it only needs to split one. I'll leave it to you to figure out how to fix that if you need to.
PHP - Split string containing newline and print
You also have to echo <br />
The HTML
<br />
element produces a line break in text (carriage-return).
It is useful for writing a poem or an address, where the division of
lines is significant.
$test = "hello \n world \n !";
foreach(explode("\n",$test) as $line){
echo $line;
echo "<br />";
}
doc: <br />
Another option is to use nl2br
to inserts HTML line breaks before all newlines in a string
$test = "hello \n world \n !";
echo nl2br($test);
Doc: nl2br()
Related Topics
PHP - Get Bool to Echo False When False
Using Jquery Ajax to Retrieve Data from MySQL
How to Measure the Speed of Code Written in PHP
Forcing a Simplexml Object to a String, Regardless of Context
PHP Echo VS PHP Short Echo Tags
How to Write a PHP Ternary Operator
How to Have a 64-Bit Integer in PHP
How to Set Order by Params Using Prepared Pdo Statement
Pdo Valid Characters For Placeholders
Get Table Column Names in MySQL
Correct File Permissions For Wordpress
How to Set Http Header to Utf-8 Using PHP Which Is Valid in W3C Validator
Get Value from Simplexmlelement Object