strstr

Find the first occurrence of a string

Syntax

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

Parameters

haystack

The string to search.

needle

The string to find.

The needle should either be explicitly cast to string, or an explicit call to chr() should be performed.

before_needle

If TRUE, strstr() returns the part of the haystack before the first occurrence of the needle (excluding the needle).

Return

Returns the portion of string, or FALSE if needle is not found.

Examples

haystack needle | string

<?

$haystack = 'CASEcase';
$needle = 's';

$return = strstr($haystack, $needle);

echo $return;

?>
se

haystack needle | chr

<?

$haystack = 'CASEcase';
$needle = chr(115);

$return = strstr($haystack, $needle);

echo $return;

?>
se

before_needle

<?

$haystack = 'CASEcase';
$needle = 's';
$before_needle = true;

$return = strstr($haystack, $needle, $before_needle);

echo $return;

?>
CASEca

Not Found

<?

$haystack = 'CASEcase';
$needle = 'not found';
$before_needle = true;

$return = strstr($haystack, $needle, $before_needle);

var_export($return);

?>
false

Internal

stristr

External

PHP

Home Menu