Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

substr_replace()

Replace a byte range selected by position, not by matching text.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

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

Replace a byte range selected by position, not by matching text. Zero length inserts; null replaces through the end, and a negative offset counts backward. With an array of input strings, offsets, lengths and replacements may be supplied per entry.

Signature

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

Example output

["AB-CD","ABX",["Cat","Dog"]]

Examples

<?php
echo json_encode([substr_replace("ABCD","-",2,0),substr_replace("ABCD","X",-2),substr_replace(["cat","dog"],["C","D"],[0,0],[1,1])]);

Result

["AB-CD","ABX",["Cat","Dog"]]

Sources