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
<?phpecho strstr("Hello NowaMagic!", "NowaMagic");?>
Program running result:
NowaMagic!
Another simple example
<?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.
<? Php $ content =$ _ POST ['content']; $ garbage = strstr ($ content, "<a"); if ($ garbage = false) {// database insert Code} else {echo "<script> alert ('your comments cannot contain link'); history. go (-1); </script> ";}?>
Well, that's probably the case.