PHP substr() Funzione
❮ Riferimento alla stringa PHP
Esempio
Restituisce "mondo" dalla stringa:
<?php
echo substr("Hello world",6);
?>
Definizione e utilizzo
La funzione substr() restituisce una parte di una stringa.
Sintassi
substr(string,start,length)
Valori dei parametri
Parameter | Description |
---|---|
string | Required. Specifies the string to return a part of |
start | Required. Specifies where to start in the string
|
length | Optional. Specifies the length of the returned string. Default is to the end of the string.
|
Dettagli tecnici
Valore di ritorno: | Restituisce la parte estratta di una stringa o FALSE in caso di errore o una stringa vuota |
---|---|
Versione PHP: | 4+ |
Registro delle modifiche: | PHP 7.0 - Se string = start (in caratteri lunghi), restituirà una stringa vuota. Le versioni precedenti restituiscono FALSE. PHP 5.2.2 - 5.2.6 - Se start ha la posizione di un troncamento negativo, viene restituito FALSE. Altre versioni ottengono la stringa dall'inizio. |
Altri esempi
Esempio
Utilizzando il parametro di avvio con diversi numeri positivi e negativi:
<?php
echo substr("Hello world",10)."<br>";
echo substr("Hello world",1)."<br>";
echo substr("Hello world",3)."<br>";
echo substr("Hello world",7)."<br>";
echo substr("Hello world",-1)."<br>";
echo substr("Hello world",-10)."<br>";
echo substr("Hello world",-8)."<br>";
echo substr("Hello world",-4)."<br>";
?>
Esempio
Utilizzando i parametri di inizio e lunghezza con diversi numeri positivi e negativi:
<?php
echo substr("Hello world",0,10)."<br>";
echo substr("Hello world",1,8)."<br>";
echo substr("Hello world",0,5)."<br>";
echo substr("Hello world",6,6)."<br>";
echo substr("Hello world",0,-1)."<br>";
echo substr("Hello world",-10,-2)."<br>";
echo substr("Hello world",0,-6)."<br>";
?>
❮ Riferimento alla stringa PHP