stristr(): finding first occurrence of a sting

Find a substring without case sensitivity

stristr() is the case-insensitive form of strstr(). It returns the matching suffix of the string or false.

$text = 'Learn PHP Strings';
echo stristr($text, 'php'); // PHP Strings

Use stristr() for case-insensitive substring extraction. For a case-sensitive search, use strstr().

$haystack="myuserid@example.com";
$needle="@";
echo stristr($haystack,$needle,false);// @example.com
Output is
@example.com
Syntax
echo stristr($haystack,$needle,$before);
$haystack
Required: Input string ( main string for searching )
$needle
Required: used to search the first occurrence inside Input string
$before
Optional: default FALSE, if set to TRUE then output returns the string before the first occurrence of $needle ( excluding the $needle )
Output is the part of string after the needle ( if $before is FALSE ) or before the needle. FALSE is returned if matching is not found.

This is case in-sensitive search. Use strstr() for case sensitive search

Example 1 of strstr()

$haystack="myuserid@example.com";
$needle="@";
echo stristr($haystack,$needle,true);// myuserid
Output is
myuserid

Example 2 of strstr()

$haystack="Welcome to plus2net.com to learn PHP";
$needle="TO";
echo stristr($haystack,$needle);// to plus2net.com to learn PHP
Output is
to plus2net.com to learn PHP

When to use stristr()

stristr() finds the first case-insensitive occurrence and returns the matching suffix. This works for searching human-readable text when differences in case should not matter:

$message = 'Learning PHP Strings';
$match = stristr($message, 'php');
if ($match !== false) {
    echo $match; // PHP Strings
}

Use strstr() for case-sensitive matching. A substring search is not a security filter: searching for https:// or <a cannot reliably identify or remove unsafe input. Validate and sanitize by the data type, and escape user-provided text on HTML output.

Searching Blank space inside a string

$string='first second';
if (stristr($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

String Functions strstr(): case-sensitive search


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer