PHP | strpos() and stripos() Functions

strpos() in PHP

This function helps us to find the position of the first occurrence of a string in another string. This returns an integer value of the position of the first occurrence of the string. This function is case-sensitive, which means that it treats upper-case and lower-case characters differently.

Syntax:



strpos(original_str, search_str, start_pos)

Parameters used:
Out of the three parameters specified in the syntax, two are mandatory and one is optional. The three parameters are described below:

  1. original_str (mandatory): This parameter refers to the original string in which we need to search the occurrence of required string.
  2. search_str (mandatory):This parameter refers to the string that we need to search.
  3. start_pos (optional): Refers to the position of the string from where the search must begin.

Return Type: This function returns an integer value which represents the index of original_str where the string search_str first occurs.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP code to search for a specific string's position
// first occurrence using strpos() case-sensitive function
function Search($search, $string){
    $position = strpos($string, $search, 5);  
    if ($position == true){
        return "Found at position: " . $position;
    }
    else{
        return "Not Found";
    }
}
  
// Driver Code
$string = "Welcome to GeeksforGeeks";
$search = "Geeks";
echo Search($search, $string);
?>

chevron_right


Output:

Found at position 11

stripos() in PHP

This function also helps us to find the position of the first occurrence of a string in another string. This returns an integer value of the position of the first occurrence of the string. This function is case-insensitive, which means it treats both upper-case and lower-case characters equally. This function works similarly as strpos() , the difference is that it is case in-sensitive where as strpos() is case sensitive.

Syntax:

stripos(original_str, search_str, start_pos)

Parameters used:
Out of the three parameters specified in the syntax, two are mandatory and one is optional

  1. original_str (mandatory): This parameter refers to the original string in which we need to search the occurrence of required string.
  2. search_str (mandatory): This parameter refers to the string that we need to find.
  3. start_pos (optional):This parameter refers to the position of the string from where the search must begin.

Return Type: This function returns an integer value which represents the index of original_str where the string search_str first occurs.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code to search for a specific string
// first occurrence using stripos() case-insensitive function
function Search($search, $string){
    $position = stripos($string, $search, 5);  
    if ($position == true){
        return "Found at posiion " . $position;
    }
    else{
        return "Not Found";
    }
}
  
// Driver Code
$string = "Welcome to GeeksforGeeks";
$search = "geeks";
echo Search($search, $string);
?>

chevron_right


Output:

Found at position 11


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.