91 of 410 menu

The strstr Function

The strstr function finds the first occurrence of a substring in a string and returns the part of the string starting from that location to the end of the string. Unlike strchr, it searches for an occurrence of a substring of several characters, not an occurrence of a single character.

There is also the stristr function, which does the same thing, but case-insensitively.

Syntax

strstr(string $haystack, string $needle, bool $before_needle = false): string|false

Example

In this example, the function will extract the page address without the domain name from the URL (it will return the substring, starting from the first /, to the end of the string)

<?php echo strstr('site.ru/dir1/dir2/page.html', '/'); ?>

Code execution result:

'/dir1/dir2/page.html'

See Also

  • the strchr function,
    which finds the first occurrence of a character and returns the remainder
  • the strrchr function,
    which finds the last occurrence of a character and returns the remainder
byenru