$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
Uses of strstr
We can find out existence of a word or combination of words in a string. For example we don't want some words to be posted by the user while submitting any message or text to the system. We can check the existence of the word by using stristr() function and know the presence of the string. This is case insensitive checking.
For a case sensitive checking we can use strstr function
stristr function is used to check the presence of https:// in the posted message. Or it can be checked the presence of <a in the posted string to identify a link. Here is the syntax of this function.
string stristr (main string , checking string)
This function returns true if the checked string is found within the main string. Now here is a code which uses stristr function and returns true after checking the word.
$main_string="This is a test string <a href=https://www.plus2net.com>with a link</a> to plus2net";
if(stristr($main_string,"https://")){
echo "String contains a link or the text https:// inside it ";}
else {
echo "String does not contains a link or the text https:// inside it ";}
$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 ";
}