In the previous article, we learned about the method of finding the position of the first occurrence of a string in another string. If you need it, please read "How to know the position of the first occurrence of a string in php》. This time we will introduce to you the method of finding the last occurrence of a string in another string. You can refer to it if you need it.
There is a function in php that can find the first occurrence of a string in another string, and there is also a function that can find the last occurrence of a string in another string. Today Let's introduce this function. However, there are two such functions, one is very sensitive to the case of characters, and the other is not sensitive to the case of characters. First, let's introduce the functions that are insensitive to the case of characters.
Let’s take a look at an example.
<?php $str = strripos("When you stare at the abyss, the Abyss is staring at you.","Abyss"); echo($str); ?>
The result is
After we have the basis of the previous article, let’s look at this function again. We know that this function is to search for strings The position of the last occurrence in another string. In this example, there are only two "abyss
", no matter it is uppercase or lowercase, it means there are only two "abyss", and the number 33
corresponds to the The first letters of two "Abyss
", so we can be sure that this function is not case-sensitive.
Let’s take a look at this function in detail.
strripos(被搜索的字符串,要查找的字符,开始搜索的位置)
This function is the same as the previous function, with three parameters, and the third parameter is optional.
In this case, let’s take a look at the return value of this function.
The same goes for this function, which returns the position of the last occurrence of a string in another string. If the string is not found, it returns FALSE. What needs more attention is that the string position starts from 0, not from 1.
Okay, we have introduced this case-insensitive function, let’s take a look at the case-sensitive function.
For the same example, let’s change to a function.
<?php $str = strrpos("When you stare at the abyss, the Abyss is staring at you.","Abyss"); echo($str); ?>
The result is
##The result of this function is also 33. Let’s take a look at the code. In this example, the word "Abyss" only appears. Once, then it stands to reason that this first time will also be the last time. Then let’s take a look at the function.strrpos(被搜索的字符串,要查找的字符,开始搜索的位置)
The above is the detailed content of How to know the last position of a string in php. For more information, please follow other related articles on the PHP Chinese website!