This article mainly introduces the difference and usage of strpos() and
stripos()
functions in PHP. I hope it will be helpful to friends who need it. help!
strpos() function
This function helps us find the first occurrence of a string in another string Location. This returns the integer value of the first occurrence of the string. This function is case-sensitive, which means it treats uppercase and lowercase characters differently.
Grammar:
strpos(original_str, search_str, start_pos)
Usage of parameters:
Of the three parameters specified in the grammar, there are two are mandatory and one is optional.
These three parameters are described as follows:
original_str (mandatory): This parameter refers to the original string in which we need to search for the occurrence of the required string.
search_str (required): This parameter refers to the string we need to search for.
start_pos (optional): means that the search must start from the position of the string.
Return type:
This function returns an integer value that represents the index of original_str where the string search_str first appears.
strpos() function usage example:
<?php function Search($search, $string){ $position = strpos($string, $search, 5); if ($position == true){ return "Found at position: " . $position; } else{ return "Not Found"; } } $string = "Welcome to PHP中文网"; $search = "PHP"; echo Search($search, $string); ?>
Output:
Found at position 11
stripos() function
This function also helps us find the first occurrence of a string in another string. This returns the integer value of the first occurrence of the string. This function is case-insensitive, which means it treats uppercase and lowercase characters equally. This function works similarly to strpos(), except that it is case-insensitive, whereas strpos() is case-sensitive.
Grammar:
stripos(original_str, search_str, start_pos)
Usage of parameters:
Among the three parameters specified in the grammar, Two are mandatory and one is optional
original_str (mandatory): This parameter refers to the original string in which we need to search for occurrences of the required string.
search_str (mandatory): This parameter refers to the string we need to find.
start_pos (optional): This parameter refers to the position where the search must start from the string.
Return type:
This function returns an integer value that represents the index of original_str where the string search_str first appears.
Stripos() function usage example:
<?php function Search($search, $string){ $position = stripos($string, $search, 5); if ($position == true){ return "Found at position " . $position; } else{ return "Not Found"; } } $string = "Welcome to PHP中文网"; $search = "PHP"; echo Search($search, $string); ?>
Output:
Found at position 11
Related recommendations: "PHP Tutorial"
The above is the detailed content of Detailed explanation of the difference and usage of strpos() and stripos() functions in php. For more information, please follow other related articles on the PHP Chinese website!