Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / PHP

substr_replace()

Sostituisce un intervallo di byte scelto per posizione.

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

Sintassi

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

Sostituisce un intervallo di byte scelto per posizione. Lunghezza zero inserisce, null sostituisce fino alla fine e un offset negativo conta all’indietro. Per un array di stringhe si possono fornire posizioni, lunghezze e sostituzioni per voce.

Firma

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

Output dell’esempio

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

Esempi

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

Risultato

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

Fonti