Home >Backend Development >PHP Tutorial >PHP string learning to determine whether a substring exists (case insensitive)

PHP string learning to determine whether a substring exists (case insensitive)

青灯夜游
青灯夜游Original
2021-08-05 12:12:072485browse

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 "子串  “&#39;$findme&#39;” 第一次出现的位置:".stripos($string, $findme);
echo "<br>子串  “&#39;$findme&#39;” 最后一次出现的位置:".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:

PHP string learning to determine whether a substring exists (case insensitive)

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 "子串  “&#39;$findme&#39;” 在字符串  “&#39;$string&#39;” 中存在。";
}else{
	echo "子串  “&#39;$findme&#39;” 在字符串  “&#39;$string&#39;” 中不存在。";
}

if(strripos($string, $findme)!=FALSE){
	echo "<br>子串  “&#39;$findme&#39;” 在字符串  “&#39;$string&#39;” 中存在。";
}else{
	echo "<br>子串  “&#39;$findme&#39;” 在字符串  “&#39;$string&#39;” 中不存在。";
}
?>

Output result:

PHP string learning to determine whether a substring exists (case insensitive)

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.

  • $stringParameter: used to specify the string to be found.

  • $findParameter: 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).

  • $startParameter: 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.

But the

$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 "子串  “&#39;$findme&#39;” 第一次出现的位置:".stripos($string, $findme,2);
echo "<br>子串  “&#39;$findme&#39;” 最后一次出现的位置:".strripos($string, $findme,-5);
?>

Output result:

PHP string learning to determine whether a substring exists (case insensitive)

Okay, that’s all. If you want to know anything else, you can click this. → →

php video tutorial

Finally, I recommend reading a classic course "

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!

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