Jump to content

Splitting post data by \n not working


unkwntech

Recommended Posts

I have a textbox that is posting a list of items (one per line) when I attempt to split that list into an array with this code:

$answers = preg_split('/[\r\n]+/', $_POST['answers'], -1, PREG_SPLIT_NO_EMPTY);

where

$_POST['answers'] = "red\ngreen\nblue";

`$answers` results in an empty array.

I have also tried

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

which resulted in an array with a single empty element.

Link to comment
https://forums.phpfreaks.com/topic/192880-splitting-post-data-by-n-not-working/
Share on other sites

Nope.

This code

$_POST['answers'] = "red\ngreen\nblue";
$answers = explode("\n", $_POST['answers']);
print_r($answers);

works just dandy for me...returning Array ( [0] => red [1] => green [2] => blue )

 

BUT:  I am a freelance PHP developer. Freelance does not mean that my work is free.  If I help that means I took time away from making money, please don't be grumpy.  And I may or may not have tested my code, if I posted it then most likely my IDE said the syntax was correct however that does not mean that it works.

 

I just had to throw that in there, because, you just never know when someone is gonna sue you for giving them code that doesn't work

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.