How to query the first occurrence of a string in php
This article mainly introduces the relevant knowledge of how php queries the position of the first occurrence of a string. The content is detailed and easy to understand, the operation is simple and fast, and it has certain reference value. I believe that after reading this article, how to query the position of the first occurrence of a string in php The article will be rewarding, let's take a look at it together.
Two query methods: 1. Use the stripos() function to find the first occurrence of a string in another string using the case-insensitive function, the syntax "stripos(the string to be searched, the string value to be queried, where to start searching)". 2. Use the strpos() function to query the case-sensitive query position for the first time, the syntax "strpos (the string to be searched, the string value to be queried, the position to start the search)".
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
Two methods for the position of the first occurrence of a php query string
Method 1: Use the stripos() function
The stripos() function finds the first occurrence of a string within another string (case-insensitive).
stripos( string , find ,start)
string: Required. Specifies the string to be searched for.
find : Required. Specifies the character to look for.
start : optional. Specifies where to start the search.
Return value: Returns the position of the first occurrence of the string in another string, or FALSE if the string is not found. Note: String positions start at 0, not 1.
<?php header( 'content-type:text/html;charset=utf-8' ); $str= "I love php, I love php too!" ; echo "Original string: " .$str. "<br>" ; $find1= "php" ; echo "Specify where the substring php appears:" .stripos($str,$find1). "<br>" ; $find2= "PHP" ; echo "Specify where the substring PHP appears:" .stripos($str,$find2); ?>
Method 2: Use the strpos() function
The strpos() function finds the first occurrence of a string within another string (case-sensitive).
strpos( string , find , start)
string: Required. Specifies the string to be searched for.
find: Required. Specifies the character to look for.
start: optional. Specifies where to start the search.
Return value: Returns the position of the first occurrence of the string in another string, or FALSE if the string is not found.
<?php header( 'content-type:text/html;charset=utf-8' ); $str= "I love php, I love php too!" ; echo "Original string: " .$str. "<br>" ; $find1= "php" ; echo "Specify where the substring php appears:" .strpos($str,$find1). "<br>" ; $find2= "PHP" ; echo "Specify where the substring PHP appears:" .strpos($str,$find2); ?>
The content of this article on "How php query string first appears" is introduced here, thank you for reading!
0 Comments