0

I have values separated by line breaks. Sometimes an empty value can be added, causing 2 PHP_EOL. But some values (only numeric) may end by 0 (Like 4950).

I am using this code :

foreach($myVar as $key => $value) {
    while(substr($value, -1, 1) == PHP_EOL) {
        $value = substr($value, -1, 1);
    }
}

But it seems like PHP_EOL == 0, cutting off the last "0" of my values (e.g. 4950 -> 495).

Try this code to figure this out :

if(PHP_EOL == 0) {
    echo "CONV0<br>";
}

if(PHP_EOL == 1) {
    echo "CONV1<br>";
}

What is the good way to remove every PHP_EOL without cutting off 0 ? Why do PHP_EOL == 0 ? That's strange !

P. Jerome
  • 283
  • 1
  • 4
  • 13

1 Answers1

1

What is the good way to remove every PHP_EOL without cutting off 0 ?

$value = rtrim($value, PHP_EOL);

See http://php.net/rtrim.

As for why PHP_EOL equals 0, see PHP type juggling, "String" == 0 and "String" == true.

deceze
  • 510,633
  • 85
  • 743
  • 889