0

I'm struggling with a behaviour in PHP and finally needed to sign up on stackoverflow as I could not find any answer to my problem for the very first time! :D

In my code, I'm getting the following content from a textarea (multiple lines):

10001;11;;;1;3;
10002;11;;;1;3;
10003;11;;;1;3;

I'm first exploding the several lines with explode("\n", $string);. Second, I explode the elements of each line: explode(";", $string[0]);

The content of key 6 is empty, but PHP only recognizes the last element of the last line as empty:

Exploding the lines with

explode("\n", $string);

Array ( [0] => 10001;11;;;1;3; [1] => 10002;11;;;1;3; [2] => 10003;11;;;1;3; )

Exploding first line with

explode(";", $string[0]);

Array ( [0] => 10001 [1] => 11 [2] => [3] => [4] => 1 [5] => 3 [6] => )

Is last element of first line empty?

( if(empty($line1[6]) )

No.

Exploding second line with

explode(";", $string[1]);

Array ( [0] => 10002 [1] => 11 [2] => [3] => [4] => 1 [5] => 3 [6] => )

Is last element of second line empty?

( if(empty($line2[6]) )

No.

Exploding third line with

explode(";", $string[2]);

Array ( [0] => 10003 [1] => 11 [2] => [3] => [4] => 1 [5] => 3 [6] => )

Is last element of third line empty?

( if(empty($line3[6]) )

Yes

Can anyone tell me, why PHP always says that only the last element of the last line is empty, although every last element is?

Thank you very much in advance!

11
  • 4
    The only thing I can think of is that the line endings are \r\n, not \n. You can use PHP_EOL instead of "\n" for your initial explode. You can see what the lines do contain if they aren't empty, with var_dump($line1[6]); Commented Jan 4, 2022 at 16:34
  • Not reproducible, using the code shown, and the assumption that lines end with \n: sandbox.onlinephpfunctions.com/code/…. But, reproducible if, as anyber suggests, the line ends are in fact \r\n: sandbox.onlinephpfunctions.com/code/… Commented Jan 4, 2022 at 16:39
  • Add your form code. And tell us browser and platform. Commented Jan 4, 2022 at 16:39
  • 1
    array_map('trim', explode("\n", $string)) Commented Jan 4, 2022 at 16:48
  • 2
    P.S. var_dump gives you more detailed feedback than print_r, including the lengths of strings - whose contents may not include any visible characters.... Commented Jan 4, 2022 at 16:48

1 Answer 1

1

The browser is normalising the line separation with carriage return line feeds. So when you split on a line feed, you still have a carriage return in the last part.

<?php
$data = $_POST['data'];

$lines = explode("\n", $data);
//$lines = preg_split('@\R@', $data);

foreach($lines as $line) {
    $parts = explode(';', $line);
    var_dump(empty($parts[6]));
}
?>
<form method='POST'>
    <textarea name='data'>10001;11;;;1;3;
10002;11;;;1;3;
10003;11;;;1;3;</textarea>
    <input type='submit'>
</form>

Output (without the form code upon form submission):

bool(false)
bool(false)
bool(true)

Swap the explode line for the preg_split one above and the output is:

bool(true)
bool(true)
bool(true)

\R escape sequence matches any Unicode newline sequence. (Please correct this definition if wrong.)

Note trailing space could be an issue. Be careful with user input.

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

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.