strstr() returns the part of the haystack beginning with the first match, or false when the needle is absent. Use a strict false check when the result controls program logic. If you only need a yes/no existence check, str_contains() is clearer for current PHP.
$text = 'Learn PHP strings';
$match = strstr($text, 'PHP');
if ($match !== false) {
echo $match; // PHP strings
}Use strstr() when you need the substring starting at the first case-sensitive match. Check for false before using the result.
$haystack="myuserid@example.com";
$needle="@";
echo strstr($haystack,$needle,false);// @example.com
Output is
@example.com
Syntax
echo strstr($haystack,$needle,$before);
$haystack$needle$before$needle ( excluding the $needle ) $haystack="myuserid@example.com";
$needle="@";
echo strstr($haystack,$needle,true);// myuserid
Output is
myuserid
$haystack="Welcome to plus2net.com to learn PHP";
$needle="to";
echo strstr($haystack,$needle);// to plus2net.com to learn PHP
Output is
to plus2net.com to learn PHP
strstr() extracts the suffix beginning at the first case-sensitive match. To extract an email domain after validating the address, search for @:
$email = 'myname@example.com';
$at = strstr($email, '@');
if ($at !== false) {
echo substr($at, 1); // example.com
}
For a case-insensitive substring match, see stristr(). Finding https:// or <a in input is not reliable link detection or input sanitization. Validate submitted URLs separately, escape text with htmlspecialchars() before HTML output, and allow HTML only through an appropriate sanitizer.
$string='first second';
if (strstr($string,' ')) {
echo " There is blank space present inside the string ";
}else{
echo " There is NO blank space present inside the string ";
}
The output is
There is blank space present inside the string
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.