How to implement string query in php: 1. Use the [strstr] function to search for the first occurrence of a string in another string; 2. Use the [strstr] function to find the first occurrence of a string in another string The first occurrence position in another string; 3. Use the [strpos] function to determine the string.
Recommended: "PHP Video Tutorial"
php determines whether the string contains the specified string Method
When writing a program, you often have to process strings. The most basic thing is to search for strings. You can use regular expressions to detect whether a string contains a specified string in PHP. If you are not familiar with regular expressions, Understand, then there are several functions that can provide convenience for you.
strstr
strstr() function searches for the first occurrence of one string within another string.
This function returns the rest of the string (from the matching point). Returns false if the searched string is not found.
The code is as follows:
<?php /*如手册上的举例*/ $email = 'user@example.com'; $domain = strstr($email, '@'); echo $domain; // prints @example.com ?>
stristr
stristr() function finds the first occurrence of a string in another string.
If successful, returns the remainder of the string (from the point of the match). If the string is not found, returns false.
It is used exactly the same as strstr. The only difference is that strstr is not case-sensitive.
strpos
The strpos function returns a boolean value. Needless to say, FALSE and TRUE .Use "===" to judge. strpos is faster than the above two functions in terms of execution speed. In addition, strpos has a parameter to specify the position of judgment, but the default is empty. It means to judge the entire string. The disadvantage is that for Chinese The support is not good.
Example 1
<php if(strpos('www.jb51.net','jb51') !== false){ echo '包含jb51'; }else{ echo '不包含jb51'; } ?>
Example 2
$str= 'abc'; $needle= 'a'; $pos = strpos($str, $needle); // 返回第一次找到改字符串的位置,这里返回为1,若查不到则返回False
explode
Use explode to judge the PHP judgment string containing code as follows:
function checkstr($str){ $needle ='a';//判断是否包含a这个字符 $tmparray = explode($needle,$str); if(count($tmparray)>1){ return true; } else{ return false; } }
5. substr For example, we need to determine whether the last character is a specified character
<?php /* $str1="<p>这是个winrar专用的dll然后下哦啊不错的dll文件,QlogWin32.dll</p>"; if(substr($str1,-8)==".dll</p>"){ echo substr($str1,0,-4); } ?>
6. substr_count counts the number of times "substring" appears 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);
function check_str($str, $substr) { $nums=substr_count($str,$substr); if ($nums>=1) { return true; } else { return false; } }
The above is the detailed content of Implementation method of php string query for existence. For more information, please follow other related articles on the PHP Chinese website!