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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.