Detailed explanation of the difference and usage of strpos() and stripos() functions in php

藏色散人
Release: 2023-04-06 07:02:02
Original
6301 people have browsed it

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!

Detailed explanation of the difference and usage of strpos() and stripos() functions in php

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)
Copy after login

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);
?>
Copy after login

Output:

Found at position 11
Copy after login
Copy after login

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)
Copy after login

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); 
?>
Copy after login

Output:

Found at position 11
Copy after login
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template