Home  >  Article  >  Backend Development  >  PHP uses strstr() function to prevent spam comments (by judging a mark)_PHP tutorial

PHP uses strstr() function to prevent spam comments (by judging a mark)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:13:07835browse

The strstr() function searches for the first occurrence of a string within another string. This function returns the rest of the string (from the matching point). Returns false if the searched string is not found.

Syntax: strstr(string,search)

Parameter string, required. Specifies the string to be searched for.
Parameter search, required. Specifies the string to be searched for. If the argument is a number, searches for characters matching the numeric ASCII value.
This function is case sensitive. For case-insensitive searches, use stristr().

Simple demonstration of strstr() function

Copy code The code is as follows:

echo strstr("Hello NowaMagic!", "NowaMagic ");
?>

Program execution result:

NowaMagic!

Another simple example

Copy code The code is as follows:

$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 execution result:

@example.com

This function can be used in many places. If your website has a lot of spam comments, and most of them have links, you need to increase backlinks, so you can use the following tips to eliminate these spam comments with links.

Copy code The code is as follows:

$content = $_POST['content'];
$garbage = strstr($content, "if($garbage == false)
{
// Database insertion code
}
else
{
echo "<script>alert('Your comment cannot contain links'); history.go(-1);</script>";
}
?>

Well, that’s probably it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313556.htmlTechArticlestrstr() 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, then...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn