Home  >  Article  >  Backend Development  >  How to query the position of a certain character in a string in php

How to query the position of a certain character in a string in php

王林
王林Original
2021-09-24 15:41:326241browse

php method to query the position of a certain character in a string: use the strpos() function to query, for example [echo strpos("You love php, I love php too!","php");] .

How to query the position of a certain character in a string in php

The operating environment of this article: windows10 system, php 7, thinkpad t480 computer.

When we learn PHP, we will definitely come into contact with a large number of strings. So what should we do if we want to find the specific position of a certain character in the string? It's actually very simple, because PHP has already provided us with functions, and we can just use them directly.

Let’s take a look at this function.

The strpos() function can find the specific position where a string appears for the first time in another string. If the string is not found, it returns FALSE.

Then let’s take a look at how this function is used.

For example, if we want to find the first occurrence of "php" in a string, the code is as follows:

<?php
echo strpos("You love php, I love php too!","php");
?>

Recommended learning: php training

The above is the detailed content of How to query the position of a certain character in 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