Home  >  Article  >  Backend Development  >  How to know the first occurrence of a string in php

How to know the first occurrence of a string in php

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-08-05 11:30:472430browse

In the previous article, we learned how to delete backslashes. If necessary, please read "How to delete backslashes in php". This time we will introduce to you the method of finding the first occurrence of a string in another string. You can refer to it if you need it.

In PHP, there is a function that can find the first occurrence of a string in another string. Today we will 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 look at a little chestnut first.

<?php 
$str = stripos("When you stare at the abyss, the Abyss is staring at you.","Abyss");
echo($str);
?>

The result of this example is

How to know the first occurrence of a string in php

In order to prevent it from matching "Abyss" instead of " abyss”, let’s count it ourselves. Start counting from 0. After counting, we find that the 22nd number is the first letter of "abyss", "a". It seems that this function really matches the first occurrence of a string in another string, and is not case-sensitive.

Let us observe this function ourselves.

stripos() function finds the first occurrence of a string within another string (case-insensitive).

Come to the syntax of Kangkang function.

stripos(被搜索的字符串,要查找的字符,开始搜索的位置)

After we know the syntax of this function, let’s talk about something we must know, that is the return value of this function.

Returns the position of the first occurrence of a string in another string, or FALSE if the string is not found. What's more important is that the string position starts from 0, not from 1.

We have finished introducing this function, let’s take a look at the case-sensitive function.

Let’s look at a small example. What will be the result of the same string and different functions?

<?php 
$str = strpos("When you stare at the abyss, the Abyss is staring at you.","Abyss");
echo($str);
?>

This result is

How to know the first occurrence of a string in php

The result is different. We don’t need to count the results one by one this time. According to the previous function From my experience, this is definitely "Abyss", not "abyss".

Let’s take a closer look at this function.

strpos(被搜索的字符串,要查找的字符,开始搜索的位置)

The syntax format and return value of this function are the same as the previous function. 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 first occurrence of a string in php. 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