Funkcja PHP substr()
Przykład
Zwróć „świat” z ciągu:
<?php
echo substr("Hello world",6);
?>
Definicja i użycie
Funkcja substr() zwraca część ciągu.
Składnia
substr(string,start,length)
Wartości parametrów
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.
|
Szczegóły techniczne
Wartość zwrotu: | Zwraca wyodrębnioną część ciągu lub FALSE w przypadku niepowodzenia lub pusty ciąg |
---|---|
Wersja PHP: | 4+ |
Dziennik zmian: | PHP 7.0 - Jeśli string = start (w znakach), zwróci pusty ciąg. Wcześniejsze wersje zwracają FALSE. PHP 5.2.2 - 5.2.6 - Jeśli start ma ujemną pozycję obcięcia, zwracane jest FALSE. Inne wersje otrzymują ciąg od samego początku. |
Więcej przykładów
Przykład
Używając parametru start z różnymi liczbami dodatnimi i ujemnymi:
<?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>";
?>
Przykład
Używając parametrów początkowych i długości z różnymi liczbami dodatnimi i ujemnymi:
<?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>";
?>
❮ Odniesienie do ciągów PHP