NOVINKA: Najžiadanejšie rekvalifikačné kurzy teraz s 50% zľavou + kurz AI ZADARMO. Nečakaj, táto ponuka dlho nevydrží! Zisti viac:

PHP - strstr

Trieda

Koreň \ Bez triedy

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ázovDátový typPredvolená hodnotaPopis
$haystackstring

Specifies the input string to be searched.

$needlemixed

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_needlebool false

If the parameter is set to true, the function returns the part of the string before the first occurrence of the searched substring (the needle). If we set it to false, the function returns the part of the string after the first occurrence of the needle (including the needle).

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
      Aktivity