How to know the last position of a string in php

醉折花枝作酒筹
Release: 2023-03-11 22:02:01
Original
3546 people have browsed it

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);
?>
Copy after login

The result is

How to know the last position of a string in php

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(被搜索的字符串,要查找的字符,开始搜索的位置)
Copy after login

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);
?>
Copy after login

The result is

How to know the last position of a string in php

##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(被搜索的字符串,要查找的字符,开始搜索的位置)
Copy after login
I found that there is no difference between this function and the previous function except for the function name, and even the return value is the same. The only difference is that this function is case-sensitive, while the above function is not case-sensitive.

That’s all. If you want to know anything else, you can click here. → →

php video tutorial

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!