Skip to content
PHP News
Search
Declined PHP 8.6

Prefix and Suffix Functions

Adds six str_prefix_* and str_suffix_* functions to ensure, remove or replace a string's prefix or suffix in one call.

Implement prefix and suffix functions as outlined in the RFC?

Primary vote · 2/3 majority

11 Yes 8 No 7 abstain 58% approval

This poll has closed.

Should the $subject parameter be the first or the last parameter in the _replace functions?

· 2/3 majority

  • Last 16
  • First 7
  • Abstain 1

This poll has closed.

The main vote failed 11 to 8, with 7 abstaining, short of the two-thirds majority it needed. The second vote, on parameter order, picked "Last" 16 to 7, with 1 abstaining, though that only mattered if the main vote passed. An earlier vote was cancelled in February 2026 to add the parameter order question. Voting closed on March 13, 2026.

Summary

A lot of PHP code adds or strips a piece at the start or end of a string, usually by checking with str_starts_with() and then cutting with substr(). It works, but it's wordy and easy to get a length wrong. This RFC from Carlos Granados proposed six small functions that do each job in one call. It was declined.

The six functions

There are three pairs, each with a prefix version and a suffix version:

  • str_prefix_ensure() and str_suffix_ensure() add the text only if it's missing.
  • str_prefix_remove() and str_suffix_remove() remove the text only if it's there.
  • str_prefix_replace() and str_suffix_replace() swap it for new text only if it's there.

Here is the before and after, using the RFC's own examples:

// Before
if (str_starts_with($host, 'www.')) {
    $host = substr($host, strlen('www.'));
}

// After
$host = str_prefix_remove($host, 'www.');           // "example.com"
$file = str_suffix_replace('.jpeg', '.jpg', $file); // "photo.jpg"
$key  = str_prefix_ensure($key, 'app:');            // "app:user:123"

How they behave

  • They always return a string, never false.
  • If nothing matches, you get the original string back with no warning.
  • Matching is exact and case-sensitive, so .jpeg won't match .JPEG.
  • They only look at the very start or end, and only once. str_suffix_remove("path///", "/") returns "path//".
  • They don't know anything about URLs or file paths. They just compare bytes.

An empty prefix or suffix is a special case. For the _replace functions it always matches, so the new text is always added.

Parameter order

People disagreed about where $subject should go in the two _replace functions. The RFC put it last, matching str_replace(), while the other four functions take it first. A second vote let voters choose.

What it means for existing code

Nothing breaks. These are six new global functions, so your code would only clash if you already defined functions with these exact names. The RFC says a GitHub search found none.