PHP strstr Definitions and usage
The
strstr () function searches for the first occurrence of a string in another string.
This function returns the remainder of the string (from the match point). returns false if the searched string is not found.
Syntax
strstr (String,search)
Parameters |
Description |
String |
Necessary. Specify the string to be searched for. |
Search |
Necessary. Specify the string to search for. If the argument is a number, searches for characters that match the numeric ASCII value. |
Tips and comments
Note: This function is binary safe.
Note: This function is sensitive to case sensitivity. For case insensitive searches, use STRISTR ().
Example
Example 1
Copy Code code as follows:
<?php
Echo strstr ("Hello world!", "World");
?>
Output:
world!
Example 2
In this case, we'll search for the characters that the ASCII value of "O" represents:
Copy Code code as follows:
<?php
Echo strstr ("Hello world!", 111);
?>
Output:
o world!
PHP uses the strstr () function to block spam comments
If your site has a lot of spam comments, most spam comments are linked because you want to add backlinks, so you can use the following tips to get rid of these linked spam comments.
Copy Code code as follows:
<?php
$content = $_post[' content '];
$garbage = Strstr ($content, "<a");
if ($garbage = = False)
{
Database Insert Code
}
Else
{
echo "<script>alert (' your comments cannot have links"); History.go ( -1);</script> ";
}
?>
PHP uses referer root access to address for jump
For example, I have a development of a yellow pages to the source of the webmaster home. Previously fixed a demo program address: Http://www.jb51.net and now this domain name needs to be used for other stations, and do not kill the original demo address How to do it? Then I can use PHP referer to judge the source if it is from the address of the webmaster download station, I will transfer it to the site domain name.
I was in http://www.jb51.net this site index.php placed the following code to get it from files.jb51.net access to my server software download Http://s.jb51.net
you can go to the demo address on this page
Copy Code code as follows:
<?php
$referHost = Parse_url ($_server[' http_referer '), php_url_host);
$validDomain = ' files.jb51.net ';
$valid = Strstr ($referHost, $validDomain) = = $validDomain;
if (!empty ($valid)) {
Echo ' <script>location.href= ' http://s.jb51.net ";</script>";
Exit
}