0

i have data return as below:

2015-07-2111
ali,21,malaysia
alan,23,england

my expected array:

0=>2015-07-2111
1=>ali,21,malaysia
2=>alan,23,england

my code :

$info = explode(",",$string);

But the code do not produce the array that i want.

2
  • 6
    $info = explode(",",$string); -> $info = explode(PHP_EOL,$string); Commented Jul 24, 2015 at 9:38
  • Why is this a comment and not an answer? Commented Jul 24, 2015 at 9:43

2 Answers 2

1

You're splitting it by , when you should be splitting it line by line, so change

$info = explode(",", $string);
//where $info becomes an array of "ali", "21", "malaysia\nalan", "23", "england"

to

$info = explode(PHP_EOL, $string);
//where $info becomes an array of "ali,21,malaysia", "alan,23,england"

as PHP_EOL is the "correct 'End Of Line' symbol for this platform"

Sign up to request clarification or add additional context in comments.

Comments

0

You can use

$str =  "2015-07-2111
ali,21,malaysia
alan,23,england";

echo '<pre>';
print_r(explode("\n",$str));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.