PHP USES the strstr of function to block spam comments by judging the a tag

  • 2020-09-16 07:24:36
  • OfStack

The strstr() function searches for the first occurrence of a string within another string. This function returns the rest of the string (from the match point). If the searched string is not found, false is returned.

Grammar: strstr (string search)

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

Simple demonstration of the strstr() function


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

Program operation Results:

NowaMagic!

Let's do 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 operation Results:

@example.com

There's a lot you can do with this function. If you have a lot of spam comments on your site, most of them are linked, because you want to add back links, so you can use the following tips to eliminate spam comments with links.


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

Well, that's about it.


Related articles: