PHP - substr
Trieda
Metóda - substr
(PHP 4, PHP 5, PHP 7)
The function returns a part of the string (a substring) according to the given parameters.
Procedurálne
- function substr (string $string, int $start, int $length) : string
Parametre
| Názov | Dátový typ | Predvolená hodnota | Popis |
|---|---|---|---|
| $string | string | The input string which part we want to be returned. It must be at least 1 character long. | |
| $start | int | The number specifying the starting position from which we want to extract the substring. The parameter can be:
| |
| $length | int | Specifies the length of the substring from the starting position (the second parameter) in the original string (the first parameter). If the parameter is not set, the substring of the starting point till the end of the original string is returned. If we enter the parameter as:
|
Mávratovej hodnoty
Vracia: string
Returns the specified part of the original string. If the returned string is
empty ("") or the function failed to return a value, it returns
false.
Príklady
In the example are shown the different outputs of the function according to the given parameters.
<?php
$text = "Chuck Norris counted to infinity - twice.";
echo substr($text, 5) . "<br>"; // Norris counted to infinity - twice.
echo substr($text, -20) . "<br>"; // to infinity - twice.
echo substr($text, 7, 12) . "<br>"; // orris counte
echo substr($text, 2, -10) . "<br>"; // uck Norris counted to infinit
echo substr($text, -20, 12) . "<br>"; // to infinity
echo substr($text, -17, -3) . "<br>"; // infinity - twi
echo substr($text, -17, -18) ? substr($text, -17, -18) . "<br>" : "Returns false <br>";
echo substr($text, 2, 0) ? substr($text, -17, -18) . "<br>" : "Returns false <br>";
Súvisiace manuály
- function mb_substr (string $str, int $start, int $length = NULL, string $encoding = mb_internal_encoding()) : string
- function preg_match (string $pattern, string $subject, array &$matches, int $flags = 0, int $offset = 0) : int
- function trim (string $str, string $character_mask = \t\n\r\0\x0B) : string
