Table of Contents

PHP RFC: Trim Form Feed

Introduction

The trim(), ltrim(), and rtrim() functions strip whitespace (or other characters) from the beginning and/or end of a string. When the second parameter ($characters) is omitted, these functions use a default set of characters.

Currently, this default set includes:

Notably missing from this list is “\f” (ASCII 12 (0x0C)), the Form Feed character.

This RFC proposes adding “\f” to the default list of characters stripped by trim(), ltrim(), and rtrim().

Proposal

The proposal is to include the Form Feed character (\f / 0x0C) in the default character mask for trim(), ltrim(), and rtrim(). Most modern programming languages treat \f as whitespace in their trimming functions. Aligning PHP with this behavior reduces cognitive load for developers working in polyglot environments.

The Form Feed character is widely recognized as whitespace.

Examples

// Current Behavior
$str = "\fHello World\f";
var_dump(trim($str));
// string(13) "\fHello World\f"
 
// Proposed Behavior
$str = "\fHello World\f";
var_dump(trim($str));
// string(11) "Hello World"

Backward Incompatible Changes

Scripts that rely on trim() preserving leading or trailing Form Feed characters will be affected.

Proposed PHP Version

PHP 8.6

Patches and Tests

References

* Internal discussion