PHP - strstr
Trieda
Metóda - strstr
(PHP 4, PHP 5, PHP 7)
The function searches for the first occurrence of a string in another string.
The function is case-sensitive and also binary-safe. For a
case-insensitive search, use the stristr() function.
Procedurálne
- function strstr (string $haystack, mixed $needle, bool $before_needle = false) : string
Parametre
| Názov | Dátový typ | Predvolená hodnota | Popis |
|---|---|---|---|
| $haystack | string | Specifies the input string to be searched. | |
| $needle | mixed | Specifies the string we want to search for. If the parameter is a number, the number is converted to the corresponding ASCII character and an occurrence of this character is searched. | |
| $before_needle | bool | false | If the parameter is set to |
Mávratovej hodnoty
Vracia: string
Returns the part of the string from first occurrence of the parameter till
the end of the string. If the searched parameter is not found,
false is returned.
Príklady
In the example below we see the different outputs according to the given parameters.
<?php
$url = "https://www.itnetwork.cz/";
// The function returns the part of the string after the occurrence of the searched string including this occurrence.
echo strstr($url, "www") . "<br>";
// If the third parameter is set to true, the function returns the part of the string before the first occurrence of the searched string.
echo strstr($url, "www", true) . "<br>";
// The function is case-sensitive.
echo strstr($url, "WWW") ? strstr($url, $www2)."<br>" : "The searched parameter was not found.<br>";
// The searched parameter can be a number.
// In the example the number 110 is converted to the character "n"
echo strstr($url, 110) . "<br>";
Súvisiace manuály
- function preg_match (string $pattern, string $subject, array &$matches, int $flags = 0, int $offset = 0) : int
