Home  >  Article  >  Backend Development  >  Example code for PHP strstr function to determine whether a string exists_PHP tutorial

Example code for PHP strstr function to determine whether a string exists_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:13:03891browse

PHP strstr Definition and Usage
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)

参数 描述
string 必需。规定被搜索的字符串。
search 必需。规定所搜索的字符串。如果该参数是数字,则搜索匹配数字 ASCII 值的字符。

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 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, you can use the following tips to eliminate spam comments with links.

Copy code The code is as follows:

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


PHP uses REFERER root access to jump to the address

For example, I developed a Yellow Pages source code and uploaded it to Webmaster's home. I previously set a demo program address: http://www.jb51.net. But now this domain name needs to be used for other sites, and what should I do if the original demo address becomes 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 the site http://www.jb51.net to allow access from files.jb51.net to locate my server’s commonly used software downloads http://s.jb51.net

You can go to the demo address on this page

Copy the 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;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313560.htmlTechArticlePHP strstr Definition and usage 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 not found...
Statement:
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