Home > Article > Backend Development > PHP uses strstr() function to prevent spam comments_PHP tutorial
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)
This function is case sensitive. For case-insensitive searches, use stristr().
<?php echo strstr("Hello NowaMagic!", "NowaMagic"); ?>
Program execution result:
NowaMagic!
<?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.