Home>Article>Backend Development> Explanation of various methods to determine whether a specified string is included in a php string

Explanation of various methods to determine whether a specified string is included in a php string

jacklove
jacklove Original
2018-06-27 17:53:27 4967browse

This article mainly introduces the various methods of whether a PHP string contains a specified string. After testing by the editor of Script House, there is no problem at all

When writing a program, you often have to deal with strings. The most basic thing is to search for strings. To detect whether a string contains a specified string in PHP, you can use regular expressions. If you don't understand regular expressions, there are several functions that can provide convenience for you.

1. strstr

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.

The code is as follows:

2. stristr

stristr() function finds a string The position of the first occurrence in another string.
If successful, return 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.

3. strpos

The strpos function returns boolean Needless to say value. 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 judgment position, but the default is empty. It means to judge the entire String. The disadvantage is that it does not support Chinese well.

Example 1

if(strpos('www.jb51.net','jb51') !== false){ echo '包含jb51'; }else{ echo '不包含jb51'; }

Example 2

$str= 'abc'; $needle= 'a'; $pos = strpos($str, $needle); // 返回第一次找到改字符串的位置,这里返回为1,若查不到则返回False

4. explode

Use explode to judge. The PHP judgment string contains the following code:

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

这是个winrar专用的dll然后下哦啊不错的dll文件,QlogWin32.dll

"; if(substr($str1,-8)==".dll

"){ echo substr($str1,0,-4); }

##6 , substr_count counts the number of times "substring" appears in the "original string"

substr_count() function is the number of times a small string appears in a large string:

$number = substr_count(big_string, small_string);
Just today I need a function to find a string. To determine whether the string big_string contains the string small_string, return true or fasle;

I checked the manual for a long time I didn't find a ready-made function, so I thought that I could 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!

For specific details, you can look up related functions for advanced applications.

Articles you may be interested in:

php framework CodeIgniter uses redis method to explain

Explanation of TCP server and client functions implemented by PHP programming

Explanation of the method of simple implementation of regular matching of provinces and cities in PHP

The above is the detailed content of Explanation of various methods to determine whether a specified string is included in a php string. For more information, please follow other related articles on the PHP Chinese website!

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