The strstr () function searches for the first occurrence of a string in another string. This function returns the rest of the string (from the matching point ). If the searched string is not found, false is returned.
Syntax: strstr (string, search)
The string parameter, required. Specifies the string to be searched.
Search parameter, required. Specifies the string to be searched. If this parameter is a number, search for characters that match the ASCII value of the number.
This function is case sensitive. Use stristr () to perform case-insensitive searches ().
A simple demonstration of strstr () Functions
Copy codeThe Code is as follows:
<? Php
Echo strstr ("Hello NowaMagic! "," NowaMagic ");
?>
Program running result:
NowaMagic!
Another simple example
Copy codeThe Code is as follows:
<? Php
$ Email = 'name @ example.com ';
$ Domain = strstr ($ email ,'@');
Echo $ domain; // prints @ example.com
// $ User = strstr ($ email, '@', true); // As of PHP 5.3.0
// Echo $ user; // prints name
?>
Program running result:
@ Example.com
This function can be used in many places. If you have a lot of spam comments on your website and most spam comments contain links, you can use the following tips to prevent spam comments with links.
Copy codeThe Code is as follows:
<? Php
$ Content = $ _ POST ['content'];
$ Garbage = strstr ($ content, "<");
If ($ garbage = false)
{
// Insert database code
}
Else
{
Echo "<script> alert ('your comments cannot be linked '); history. go (-1); </script> ";
}
?>
Well, that's probably the case.