PHP uses the strstr () function to block spam comments (by judging the mark)

Source: Internet
Author: User

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

Copy codeThe Code is as follows:
<? Php
Echo strstr ("Hello NowaMagic! "," NowaMagic ");
?>

Program running result:

NowaMagic!

Another simple example

Copy codeThe Code is as follows:
<? 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.

Copy codeThe Code is as follows:
<? Php
$ Content = $ _ POST ['content'];
$ Garbage = strstr ($ content, "<");
If ($ garbage = false)
{
// Insert database code
}
Else
{
Echo "<script> alert ('your comments cannot be linked '); history. go (-1); </script> ";
}
?>

Well, that's probably the case.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.