str_replace

FunctionParams and return types changed in PHP 8.0

Replace all occurrences of the search string with the replacement string.

PHP 5
PHP 7
PHP 8.0
Improved
PHP 8.1
PHP 8.2-8.3
PHP 8.4
PHP 8.5
PHP 8.6

str_replace Function synopsis

str_replace(
    array|string $search,
    array|string $replace,
    array|string $subject,
    &$count = null
  ): array|string

Parameters

Typearray|string

The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.

$replace

Typearray|string

The replacement value that replaces found $search values. An array may be used to designate multiple replacements.

$subject

Typearray|string

The string or array being searched and replaced on, otherwise known as the haystack.

$count

Passed by reference, OptionalDefault valuenull

If passed, this will be set to the number of replacements performed.

Note that the parameter $count is passed by reference, and contains additional/updated context data that may be useful.

Return value

Typearray|string

This function returns a string or an array with the replaced values.

Changes to the str_replace Function

PHP 8.0

  • Return type added: array|string
  • Parameter type added for parameter #1 ($search): array|string
  • Parameter type added for parameter #2 ($replace): array|string
  • Parameter type added for parameter #3 ($subject): array|string
  • Parameter name of parameter #4 changed: $replace_count to $count
  • Parameter default value added for position #4 ($count): null
  str_replace(
-     $search,
+     array|string $search,
-     $replace,
+     array|string $replace,
-     $subject,
+     array|string $subject,
-     &$replace_count
+     &$count = null
-   )
+   ): array|string

str_replace Function Availability

PHP VersionAvailability
PHP 8.6Upcoming Release Yes
PHP 8.5Supported (Latest) Yes
PHP 8.4Supported Yes
PHP 8.3Security-Fixes Only Yes
PHP 8.2Security-Fixes Only Yes
PHP 8.1Unsupported Yes
PHP 8.0Unsupported Yes
PHP 7.4Unsupported Yes
PHP 7.3Unsupported Yes
PHP 7.2Unsupported Yes
PHP 7.1Unsupported Yes
PHP 7.0Unsupported Yes
PHP 5.6Unsupported Yes
PHP 5.5Unsupported Yes
PHP 5.4Unsupported Yes
PHP 5.3Unsupported Yes