PHP 8 reference · String Functions
PHP strstr() Function
Return the part of a string from the first occurrence of a substring to the end, or the part before it.
PHP 8 Since 4.0
Syntax
strstr(string $haystack, string $needle, bool $before_needle = false): string|false
Parameters
| Parameter | Description |
|---|---|
haystack |
The string to search in. |
needle |
The string to find. |
before_needle optional |
true returns the part before the match instead. Default: false |
Return value
The matching part, or false if the needle isn't found.
Examples
<?php
$email = "[email protected]";
echo strstr($email, "@"), "\n";
echo strstr($email, "@", true), "\n";
Output
@example.com ada
<?php
var_dump(strstr("no-at-sign", "@"));
Output
bool(false)
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
In other languages
str.partition()· Python
Related
strrchr()· Return the part of a string from the last occurrence of a character to the end. Only the first character of the needle is used.strpos()· Find the position of the first occurrence of a substring. Returns false if it's not found, so compare with === false.explode()· Split a string into an array by a separator, like turning "a,b,c" into ["a", "b", "c"]. A limit caps the pieces.- PHP Strings · tutorial