-->

Explode PHP string by new line

2019-01-02 16:46发布

问题:

Simple, right? Well, this isn't working :-\

$skuList = explode('\n\r', $_POST['skuList']);

回答1:

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 (three down) to cover all your bases (and upvote his answer):

$skuList = preg_split('/\r\n|\r|\n/', $_POST['skuList']);


回答2:

Cover all cases. Don't rely that your input is coming from a Windows environment.

$skuList = preg_split("/\\r\\n|\\r|\\n/", $_POST['skuList']);

or

$skuList = preg_split('/\r\n|\r|\n/', $_POST['skuList']);


回答3:

Try "\n\r" (double quotes) or just "\n".

If you're not sure which type of EOL you have, run a str_replace before your explode, replacing "\n\r" with "\n".



回答4:

Lots of things here:

  • You need to use double quotes, not single quotes, otherwise the escaped characters won't be escaped.
  • The normal sequence is \r\n, not \n\r.
  • Depending on the source, you may just be getting \n without the \r (or even in unusual cases, possibly just the \r)

Given the last point, you may find preg_split() using all the possible variants will give you a more reliable way of splitting the data than explode(). But alternatively you could use explode() with just \n, and then use trim() to remove any \r characters that are left hanging around.



回答5:

this php function explode string by newline

Attention : new line in Windows is \r\n and in Linux and Unix is \n
this function change all new lines to linux mode then split it.
pay attention that empty lines will be ignored

function splitNewLine($text) {
    $code=preg_replace('/\n$/','',preg_replace('/^\n/','',preg_replace('/[\r\n]+/',"\n",$text)));
    return explode("\n",$code);
}

example

$a="\r\n\r\n\n\n\r\rsalam\r\nman khobam\rto chi\n\rche khabar\n\r\n\n\r\r\n\nbashe baba raftam\r\n\r\n\r\n\r\n";
print_r( splitNewLine($a) );

output

Array
(
    [0] => salam
    [1] => man khobam
    [2] => to chi
    [3] => che khabar
    [4] => bashe baba raftam
)


回答6:

try

explode(chr(10), $_POST['skuList']);


回答7:

For a new line, it's just

$list = explode("\n", $text);

For a new line and carriage return (as in Windows files), it's as you posted. Is your skuList a text area?



回答8:

Place the \n in double quotes:

explode("\n", $_POST['skuList']);

In single quotes, if I'm not mistaken, this is treated as \ and n separately.



回答9:

Have you tried using double quotes?



回答10:

Not perfect but I think it must be safest. Add nl2br:

$skuList = explode('<br />', nl2br($_POST['skuList']));


回答11:

Try this:

explode(PHP_EOF, $lines);


回答12:

First of all, I think it's usually \r\n, second of all, those are not the same on all systems. That will only work on windows. It's kind-of annoying trying to figure out how to replace new lines because different systems treat them differently (see here). You might have better luck with just \n.



回答13:

If anyone else tried this but it wasn't working, this is a reminder that you might have done the same brain fart as I.

Have you mysql escaped the string first? In this case newline character is no longer a newline character.

I didn't do anything to avoid parsing it, just adapted and exploded by '\n' (literally backslash and n rather than actual newline character.



回答14:

Losing line breaks from posting from input textboxes?
What works faster for me is to copy paste any text or Excel or HTML table type or newline type of data and paste it into a textarea instead of an inputextbox: this keeps the linebreaks intact in the POST.

 <textarea  id="txtArea" name="txtArea" rows="40" cols="170"></textarea>
 <br>
 <input type="submit" value="split lines into array" /> 

in the form receiving file:

 $txtArea ='';  
 $txtArea = $_POST['txtArea'];  
 $TA = $_POST['txtArea'];  
 $string = $TA;  
 $array = preg_split ('/$\R?^/m', $string); 
// or any of these: 
// $array = explode(PHP_EOL,$string);  
// $array = explode("\n", $txtArea); 
 echo "<br>A0: ".$array[0];
 echo "<br>A1: ".@$array[1];
 echo "<br>A2: ".@$array[2];


回答15:

This method always works for me:

$uniquepattern="@#$;?:~#abcz"//Any set of characters which you dont expect to be present in user input $_POST['skuList'] better use atleast 32 charecters.
$skuList=explode($uniquepattern,str_replace("\r","",str_replace("\n",$uniquepattern,$_POST['skuList'])));


回答16:

PHP_EOL is ostensibly used to find the newline character in a cross-platform-compatible way, so it handles DOS/Unix issues.

Try this:

$myString = "Prepare yourself to be caught
You in the hood gettin' shot
We going throw hell of blows
got my whole frame froze";

$myArray = explode(PHP_EOL, $myString);

print_r($myArray);


回答17:

You can try it with regex:

$skuList = explode('/[\r\n]+/', $_POST['skuList']);



标签: