Home  >  Article  >  Backend Development  >  PHP uses strstr() function to prevent spam comments_PHP tutorial

PHP uses strstr() function to prevent spam comments_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:33:12713browse

strstr() function searches for the first occurrence of one 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

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

Program execution 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 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.

<?php
$content = $_POST['content'];
$garbage = strstr($content, "<a");
if($garbage == false)
{
	// 数据库插入代码
}
else
{
	echo "<script>alert('你的评论不能带有链接'); history.go(-1);</script>";
}
?>

Well, that’s probably it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752525.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