HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

substr_replace

Description

The substr_replace of String for PHP replace text within a portion of a string.

Syntax

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

Parameters

string

The input string.

An array of strings can be provided, in which case the replacements will occur on each string in turn. In this case, the replace, offset and length parameters may be provided either as scalar values to be applied to each input string in turn, or as arrays, in which case the corresponding array element will be used for each input string.

replace

The replacement string.

offset

If it is negative, the replacing will begin at the offset'th character from the end of string.

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

length

If it is omitted, then it will default to strlen( string ); i.e. end the replacing at the end of string.

If it is negative, it represents the number of characters from the end of string at which to stop replacing.

If it is zero, this function will have the effect of inserting replace into string at the given offset offset.

If it is positive, it represents the length of the portion of string which is to be replaced.

Return

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

Examples

1 · string replace offset · negative

<?

$string = "abcdefghijklmnopqrstuvwxyz";
$replace = "12345";
$offset = -5;

$return = substr_replace($string, $replace, $offset);

echo $return;
abcdefghijklmnopqrstu12345

2 · string replace offset · zero

<?

$string = "abcdefghijklmnopqrstuvwxyz";
$replace = "12345";
$offset = 0;

$return = substr_replace($string, $replace, $offset);

echo $return;
12345

3 · string replace offset · positive

<?

$string = "abcdefghijklmnopqrstuvwxyz";
$replace = "12345";
$offset = 5;

$return = substr_replace($string, $replace, $offset);

echo $return;
abcde12345

4 · length · negative

<?

$string = "abcdefghijklmnopqrstuvwxyz";
$replace = "12345";
$offset = 5;
$length = -5;

$return = substr_replace($string, $replace, $offset, $length);

echo $return;
abcde12345vwxyz

5 · length · zero

<?

$string = "abcdefghijklmnopqrstuvwxyz";
$replace = "12345";
$offset = 5;
$length = 0;

$return = substr_replace($string, $replace, $offset, $length);

echo $return;
abcde12345fghijklmnopqrstuvwxyz

6 · length · positive

<?

$string = "abcdefghijklmnopqrstuvwxyz";
$replace = "12345";
$offset = 5;
$length = 5;

$return = substr_replace($string, $replace, $offset, $length);

echo $return;
abcde12345klmnopqrstuvwxyz

7 · array

<?

$string =
[
    "abcdefghijklmnopqrstuvwxyz",
    "abcdefghijklmnopqrstuvwxyz",
    "abcdefghijklmnopqrstuvwxyz",
    "abcdefghijklmnopqrstuvwxyz",
    "abcdefghijklmnopqrstuvwxyz",
    "abcdefghijklmnopqrstuvwxyz"
];
$replace =
[
    "12345",
    "12345",
    "12345",
    "12345",
    "12345",
    "12345"
];
$offset =
[
    -5,
    0,
    5,
    5,
    5,
    5
];
$length =
[
    strlen($string[0]),
    strlen($string[1]),
    strlen($string[2]),
    -5,
    0,
    5
];

$return = substr_replace($string, $replace, $offset, $length);

print_r($return);
Array
(
    [0] => abcdefghijklmnopqrstu12345
    [1] => 12345
    [2] => abcde12345
    [3] => abcde12345vwxyz
    [4] => abcde12345fghijklmnopqrstuvwxyz
    [5] => abcde12345klmnopqrstuvwxyz
)