The PHP strstr function determines whether a string exists

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

PHP strstr definition and usage
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 describe string A necessity. Specifies the string to be searched for. search A necessity. Specifies the string to search for. If the parameter is a number, the character matching the number ASCII value is searched.

Hints and comments

Note: This function is base 2 safe.

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

example
Example 1


<?php
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" :


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

Output:

o world!

PHP USES the strstr() function to block spam comments

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>"; 
} 
?> 


PHP USES REFERER root residence access to redirect addresses

For example, I have a development of a yellow Pages source to the home of the webmaster. Before ordered a demo address: https: / / www ofstack. com now the domain name needs to be used as other station, don't kill again at the original demo address what failure. Then I can use PHP REFERER to determine the source if it is the address of the downloaded site from the webmaster and I will transfer it to the domain name of the site.

. I am in https: / / www ofstack. com index. On this site php placed in the following code. Let it come from img jbzj. Commonly used com access to my server software download http: / / s ofstack. com

You can go to the demo address on this page

 
<?php 
$referHost = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); 
$validDomain = 'img.jbzj.com'; 
$valid = strstr($referHost, $validDomain) == $validDomain; 
if(!empty($valid)){ 
echo '<script>location.href="http://s.ofstack.com";</script>'; 
exit; 
} 


Related articles: