Open In App

PHP strpos() and stripos() Functions

Last Updated : 09 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to find the position of the first occurrence of a string in another string using strpos() and stripos() Functions in PHP, & will see their implementation through the examples.

Both the strpos() and stripos() Functions in PHP are binary-safe which means the function will handle its input as a raw byte stream and disregard every textual content it may contain. Here, the function will work correctly while passing arbitrary binary data ie., a string having non-ASCII bytes &/or null-bytes.

strpos() Function: 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);

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

  • original_str: This is a mandatory parameter that refers to the original string in which we need to search the occurrence of the required string.
  • search_str: This is a mandatory parameter that refers to the string that we need to search.
  • start_pos: This is an optional parameter that refers to the position of the string from where the search must begin.

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

Example: This example illustrates the strpos() function that specifies the position of the occurrence of a string in another string.

PHP




<?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 (is_numeric($position))
        {
            return "Found at position: " . $position;
        }
        else
        {
            return "Not Found";
        }
    }
     
    // Driver Code
    $string = "Welcome to GeeksforGeeks";
    $search = "Geeks";
    echo Search($search, $string);
?>


Output:

Found at position 11

stripos() Function: 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 whereas strpos() is case sensitive.

Syntax:  

stripos(original_str, search_str, start_pos);

Parameter value: Out of the three parameters specified in the syntax, two are mandatory and one is optional.

  • original_str: This is a mandatory parameter that refers to the original string in which we need to search the occurrence of the required string.
  • search_str: This is a mandatory parameter that refers to the string that we need to find.
  • start_pos: This is an optional parameter that refers to the position of the string from where the search must begin.

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

Example: This example illustrates the stripos() function that specifies the position of the occurrence of a string in another string.

PHP




<?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 position " . $position;
        }
        else
        {
            return "Not Found";
        }
    }
     
    // Driver Code
    $string = "Welcome to GeeksforGeeks";
    $search = "geeks";
    echo Search($search, $string);
?>


Output: 

Found at position 11

Let us understand these functions in a tabular form -:

  strpos() stripos() 
1. The strpos() function finds the position of the first occurrence of a string inside another string. The stripos() function finds the position of the first occurrence of a string inside another string.
2. It is case-sensitive function. It is case-insensitive function.
3.

Its syntax is -:

strpos(string,find,start)

Its syntax is -:

stripos(string,find,start)

4. This function returns the position of the first occurrence of a string inside another string else it returns false if the string does not found. It returns the position of the first occurrence of a string inside another string else it returns false if the string does not found.
5. It is supported in PHP version 4.0+ It is supported in PHP version 5.0+


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads