This article focuses on whether or not the PHP string contains a string of the specified number of methods, tested completely no problem
When writing a program, often to deal with strings, the most basic is the search string, whether the PHP detection string contains the specified string can use the regular, if you do not understand the regular, then there are several functions can provide you with convenience.
1. Strstr
The Strstr () function searches for the first occurrence of a string in another string.
The function returns the remainder of the string (from the matching point). returns false if the searched string is not found.
The code is as follows:
<?php/* For example in manual */$email = ' user@example.com '; $domain = Strstr ($email, ' @ '); Echo $domain; Prints @example .com?>
2. Stristr
The Stristr () function finds the position of the first occurrence of a string in another string.
If successful, returns the remainder of the string (from the matching point). If the string is not found, false is returned.
It is exactly the same as strstr. The only difference is that the STRISTR is not case sensitive.
3. Strpos
The Strpos function returns a Boolean value. False and true do not say much. use "= = =" to judge. Strpos is faster than the above two functions in execution speed, and Strpos has a parameter to specify the location of the judgment, but the default is empty. Meaning to judge the entire string. The disadvantage is that the support for Chinese is not good.
Example 1
if (Strpos (' www.jb51.net ', ' jb51 ')!== false) { echo ' contains jb51 ';} else{Echo ' does not contain jb51 ';}
Example 2
$STR = ' abc '; $needle = ' a '; $pos = Strpos ($str, $needle); Returns the position where the string was first found, returned as 1, false if not detected
4. Explode
Using explode to determine the PHP judgment string contains the following code:
function Checkstr ($str) {$needle = ' a ';//Determine if the character "A" is included $tmparray = Explode ($needle, $str); if (count ($tmparray) >1) { return true;} else{ return false;}}
5, substr For example we need to judge whether the last character is a character set
<?php/* $str 1= "<p> This is a winrar dedicated DLL and then under OH nice DLL file,qlogwin32.dll</p>"; if (substr ($str 1,-8) = = ".dll< /p> ") {echo substr ($str 1,0,-4);}
6, Substr_count statistics "substring" in "The number of occurrences in the original string"
The Substr_count () function is the number of times a small string appears in a large string:
$number = Substr_count (big_string, small_string);
Exactly today need a function to find a string, to implement the Big_string whether the string contains a string small_string, returns True or Fasle;
Checking for a half-day manual did not find a ready-made function, so thought can use the Substr_count function to implement the code as follows:
function Check_str ($STR, $substr) {$nums =substr_count ($str, $substr); if ($nums >=1) { return true; } else { return false;} }
Super simple!
Specifically, you can find the relevant functions for advanced applications.