substr_replace

FunctionParams and return types changed in PHP 8.0

Replace text within a portion of a 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

substr_replace Function synopsis

substr_replace(
    array|string $string,
    array|string $replace,
    array|int $offset,
    array|int|null $length = null
  ): array|string

Parameters

$string

Typearray|string

The input string.

$replace

Typearray|string

The replacement string.

$offset

Typearray|int

If $offset is non-negative, the replacing will begin at the $offset'th offset into $string.

$length

OptionalTypearray|int|nullDefault valuenull

If given and is positive, it represents the length of the portion of $string which is to be replaced. If it is negative, it represents the number of characters from the end of $string at which to stop replacing. If it is not given, then it will default to strlen( $string ); i.e. end the replacing at the end of $string. Of course, if $length is zero then this function will have the effect of inserting $replace into $string at the given $offset offset.

Return value

Typearray|string

The result string is returned. If $string is an array then array is returned.

Changes to the substr_replace Function

PHP 8.0

  • Return type added: array|string
  • Parameter name of parameter #1 changed: $str to $string
  • Parameter type added for parameter #1 ($string): array|string
  • Parameter type added for parameter #2 ($replace): array|string
  • Parameter name of parameter #3 changed: $start to $offset
  • Parameter type added for parameter #3 ($offset): array|int
  • Parameter type added for parameter #4 ($length): array|int|null
  • Parameter default value added for position #4 ($length): null
  substr_replace(
-     $str,
+     array|string $string,
-     $replace,
+     array|string $replace,
-     $start,
+     array|int $offset,
-     $length
+     array|int|null $length = null
-   )
+   ): array|string

substr_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