Two query methods: 1. Use the stripos() function to find the first occurrence of a string in another string in a case-insensitive manner. The syntax "stripos (the searched string, to The string value of the query, the position to start the search)". 2. Use the strpos() function to query the first occurrence position of the case-sensitive search. The syntax is "strpos (string to be searched, string value to be queried, position to start searching)".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php query string Two methods for the first occurrence of the position
Method 1: Use the stripos() function
stripos() function to find the string in another string The position of the first occurrence in a string (case-insensitive).
stripos(string,find,start)
string: Required. Specifies the string to be searched for.
#find: Required. Specifies the characters to search for.
#start: Optional. Specifies the location from which to start the search.
Return value: Returns the position of the first occurrence of a string in another string. If the string is not found, FALSE is returned. Note: The string position starts from 0, not from 1.
<?php header('content-type:text/html;charset=utf-8'); $str="I love php, I love php too!"; echo "原字符串:".$str."<br>"; $find1="php"; echo "指定子串php的出现位置:".stripos($str,$find1)."<br>"; $find2="PHP"; echo "指定子串PHP的出现位置:".stripos($str,$find2); ?>
Method 2: Use the strpos() function
strpos() function to find the first string in another string The position of an occurrence (case sensitive).
strpos(string,find,start)
string: Required. Specifies the string to be searched for.
#find: Required. Specifies the characters to search for.
start: Optional. Specifies the location from which to start the search.
Return value: Returns the position of the first occurrence of a string in another string. If the string is not found, FALSE is returned.
<?php header('content-type:text/html;charset=utf-8'); $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); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to query the first occurrence of a string in php. For more information, please follow other related articles on the PHP Chinese website!