Home > php教程 > php手册 > body text

Example code for PHP strstr function to determine whether a string exists_php basics

WBOY
Release: 2016-05-16 09:00:10
Original
1738 people have browsed it

PHP strstr Definition and Usage
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). Returns false if the searched string is not found.

Syntax

strstr(string,search)

Parameters Description
string Required. Specifies the string to be searched for.
search Required. Specifies the string to search for. If the argument is a number, then searches for characters matching the numeric ASCII value.

Tips and Notes

Note: This function is binary safe.

Note: This function is case-sensitive. For case-insensitive searches, use stristr().

Example
Example 1

Copy code The code is as follows:

echo strstr("Hello world!","world");
?>

Output:

world!

Example 2
In this example, we will search for the character represented by the ASCII value of "o":

Copy code The code is as follows:

echo strstr("Hello world!",111);
?>

Output:

o world!

PHP uses the strstr() function to prevent spam comments

If your website has a lot of spam comments, most of the spam comments have links. , because you want to increase backlinks, so you can use the following tips to eliminate these spam comments with links.

Copy code The code is as follows:


PHP uses REFERER root access to jump to the address

For example, I developed a Yellow Pages source code and transferred it to the webmaster's home. I have set a demo program address before: But now this domain name needs to be used for other sites, and what should I do if I don’t want the original demo address to become invalid? Then I can use PHP REFERER to determine the source. If it comes from the address of the webmaster's download station, I will transfer it to the site domain name.

I placed the following code in index.php on this site to allow access from files.jb51.net to locate my server's commonly used software download http://s.jb51.net

You can go to the demo address on this page

Copy code The code is as follows:

$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;
}
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template