You need to use "\n\r"
instead of PHP_EOL
or space
$row = explode("\n\r",$row);
$row = $row[0];
preg_match('/( +)$/',$row,$matches);
Now as you see you can use space in pattern for regExp
Question
The code below produces an unexpected result:
$row = 'one //There are spaces here
two ';
$row = explode(PHP_EOL,$row);
$row = $row[0];
preg_match('/( +)$/',$row,$matches);
When using var_dump($matches);
, the output is:
array(0) {
}
But, it should be something like this:
array(2) {
[0]=>string(8) " "
[1]=>string(8) " "
}
PHP_EOL
with "\n"
makes no difference.Using preg_match('/(\s+)$/',$row,$matches);
produces the expected result:
array(2) {
[0]=>string(8) " "
[1]=>string(8) " "
}
The expected also happens when using:
$row = 'one ';
preg_match('/( +)$/',$row,$matches);
But, obviously, these both have their own reasons to not be used.
My question is: Why does PHP not recognize the spaces as spaces but only as whitespace?
Example: http://sandbox.onlinephpfunctions.com/code/a6f3ea8b422671d86a37b765986395b1dd6f94e8
Solution
You need to use "\n\r"
instead of PHP_EOL
or space
$row = explode("\n\r",$row);
$row = $row[0];
preg_match('/( +)$/',$row,$matches);
Now as you see you can use space in pattern for regExp