Since PHP 8.6, the trim, ltrim and rtrim functions remove the form feed character (\f) by default. The default list of trimmed characters is now " \f\n\r\t\v\x00". Before PHP 8.6, the form feed was not part of this list and had to be passed explicitly as the second argument.
The following code trims a string which starts and ends with a form feed. The bin2hex function is used, because the form feed character is not visible in the output:
<?php
$value = "\fhello\f";
echo bin2hex(trim($value)).PHP_EOL;
echo bin2hex(ltrim($value)).PHP_EOL;
echo bin2hex(rtrim($value)).PHP_EOL;
Before PHP 8.6:
0c68656c6c6f0c
0c68656c6c6f0c
0c68656c6c6f0c
Since PHP 8.6:
68656c6c6f
68656c6c6f0c
0c68656c6c6f
The 0c value is the form feed character and 68656c6c6f is the hello string. Since PHP 8.6, the trim function removes the form feed from both ends, ltrim from the start and rtrim from the end. Before PHP 8.6, all three functions returned the string unchanged.
To keep the previous behavior, the list of characters must be specified explicitly (trim($value, " \n\r\t\v\x00")).
Leave a Comment
Cancel reply