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

How to query the occurrence position of a string in php

青灯夜游
青灯夜游Original
2022-07-29 19:16:027222browse

4 methods: 1. Use stripos() to return the position of the first occurrence (not case sensitive), the syntax is "stripos(string, query value)"; 2. Use strpos(), Return the position of the first occurrence (case-sensitive), the syntax is "strpos(string, query value)"; 3. Use strripos() to return the position of the last occurrence, the syntax is "strripos(string, query value)" ;4. Use strrpos(), the syntax is "strrpos(string, query value)".

How to query the occurrence position of a string in php

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer

Method 1: Use stripos() Function

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

Return value: Returns the position of the first occurrence of a string in another string, or FALSE if the string is not found. Note: The string position starts from 0, not from 1.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str="Hello world!";
echo "原字符串:".$str."<br>";
$find="world";
echo "指定子串world的出现位置:".stripos($str,$find);
?>

How to query the occurrence position of a string in php

Method 2: Use the strpos() function

strpos() function to find the first string in another string The position of an occurrence (case sensitive).

Return value: Returns the position of the first occurrence of a string in another string, or FALSE if the string is not found.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str="I love php, I love php too!";
echo "原字符串:".$str."<br>";
$find1="php";
echo "指定子串php的出现位置:".strpos($str,$find1)."<br>";
$find2="PHP";
echo "指定子串PHP的出现位置:".strpos($str,$find2);
?>

How to query the occurrence position of a string in php

Method 3: Use strripos() function

strripos() function finds the last string in another string The position of one occurrence (case-insensitive).

Return value: Returns the position of the last occurrence of a string in another string, or FALSE if the string is not found.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str="I love php, I love php too!";
echo "原字符串:".$str."<br>";
$find1="php";
echo "指定子串php的出现位置:".strripos($str,$find1)."<br>";
$find2="PHP";
echo "指定子串PHP的出现位置:".strripos($str,$find2);
?>

How to query the occurrence position of a string in php

Method 4: Use the strrpos() function

strrpos() function to find the last string in another string The position of an occurrence (case sensitive).

Return value: Returns the position of the last occurrence of a string in another string, or FALSE if the string is not found.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str="I love php, I love php too!";
echo "原字符串:".$str."<br>";
$find1="php";
echo "指定子串php的出现位置:".strrpos($str,$find1)."<br>";
$find2="PHP";
echo "指定子串PHP的出现位置:".strrpos($str,$find2);
?>

How to query the occurrence position of a string in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to query the occurrence position 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