Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / PHP

substr()

Estrae una porzione di byte. Un offset negativo conta dalla fine, una lunghezza negativa esclude byte finali.

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

Sintassi

substr(string $string, int $offset, ?int $length = null): string

Estrae una porzione di byte. Un offset negativo conta dalla fine, una lunghezza negativa esclude byte finali. Senza lunghezza o con null prende il resto. Un inizio oltre la fine dà la stringa vuota, uno prima dell’inizio viene portato a zero. Può tagliare un carattere UTF-8.

Firma

substr(string $string, int $offset, ?int $length = null): string

Output dell’esempio

["CD","BCD","","AB","c3"]

Esempi

<?php
echo json_encode([substr("ABCDE",-3,2),substr("ABCDE",1,-1),substr("ABCDE",9),substr("ABC",-9,2),bin2hex(substr("\u{00E9}",0,1))]);

Risultato

["CD","BCD","","AB","c3"]

Fonti