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

PHP uses strstr() function to prevent spam comments_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:33:12
Original
750 people have browsed it

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");
?>
Copy after login

Program execution result:

NowaMagic!
Copy after login

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
?>
Copy after login

Program execution result:

@example.com
Copy after login

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>";
}
?>
Copy after login

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...
Related labels:
php
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template