String comparison and search are the most common operations. In the previous article "PHP String Learning: Comparing Two Strings", we introduced the method of comparing strings. The following article will take you through the string search operation and introduce the method of checking whether a substring exists.
When performing a string search operation, sometimes it is required to search for a specified substring (referred to as a substring) in a certain string to see if the substring exists in the string.
We generally use PHP built-in functions to find the first or last occurrence of this substring in the string to make a judgment. There are two situations for searching strings: one is case-insensitive, that is, case-insensitive search; the other is case-sensitive, that is, case-sensitive search. Now let's take a look at how to determine whether a substring exists without case sensitivity.
Let’s take a look at the chestnuts below.
<?php header("Content-type:text/html;charset=utf-8"); $string = "ABCDCBAbcd"; $findme = "bC"; echo "子串 “'$findme'” 第一次出现的位置:".stripos($string, $findme); echo "<br>子串 “'$findme'” 最后一次出现的位置:".strripos($string, $findme); ?>
Look at the code in the above example, what do you think the result will be? The first occurrence is "2" and the last occurrence is "8", right? Let's take a look at the output:
Oh, it's actually "1" and "7", why is this? The reason is: The string position starts at 0, not 1. Of course it is wrong for us to calculate based on the starting position 1.
So we can use the following code to determine whether the substring exists
<?php header("Content-type:text/html;charset=utf-8"); $string = "ABCDCBAbcd"; $findme = "bC"; if(stripos($string, $findme)!=FALSE){ echo "子串 “'$findme'” 在字符串 “'$string'” 中存在。"; }else{ echo "子串 “'$findme'” 在字符串 “'$string'” 中不存在。"; } if(strripos($string, $findme)!=FALSE){ echo "<br>子串 “'$findme'” 在字符串 “'$string'” 中存在。"; }else{ echo "<br>子串 “'$findme'” 在字符串 “'$string'” 中不存在。"; } ?>
Output result:
Let’s take a look at it in detail These two functions.
stripos($string,$find,$start)
The function can find the first occurrence of a string in another string (case-insensitive).
strripos($string,$find,$start)
The function can find the last occurrence of a string in another string (case-insensitive).
The parameters of these two functions are similar. They both accept two required parameters $string
and $find
, and one optional parameter $start
.
$string
Parameter: used to specify the string to be found.
$find
Parameter: used to specify the substring to be found, which can contain one or more characters. (If it is not a string type, it is converted to an integer and treated as a character-ordered value).
$start
Parameter: used to specify which character in $string
to start searching from. The returned positional numerical value is still relative to # The starting position of ##$string.
$start parameter of the strripos() function allows negative values, which will cause the search to start from the beginning of the string to
$start position.
<?php header("Content-type:text/html;charset=utf-8"); $string = "ABCDCBAbcd"; $findme = "bC"; echo "子串 “'$findme'” 第一次出现的位置:".stripos($string, $findme,2); echo "<br>子串 “'$findme'” 最后一次出现的位置:".strripos($string, $findme,-5); ?>
PHP String Processing (Jade Girl Heart Sutra Edition)", it's free~ come and learn !
The above is the detailed content of PHP string learning to determine whether a substring exists (case insensitive). For more information, please follow other related articles on the PHP Chinese website!