PHP strpos Function | What is strpos in PHP and How to use to find string
strpos Summary: in this tutorial, you will learn What is PHP strpos Function and How to use to find the position of the first occurrence of a string in another string.
Definition and Usage PHP strpos() Function
This function helps us to find out the position of the first occurrence of a string in another string. This function returns an integer value of the position of the first occurrence of the string. also, this function is case-sensitive, which implies that it treats upper-case and lower-case characters differently.
This function is binary-safe.
Related functions:
strrpos() – Finds the position of the last occurrence of a string inside another string. this is a case-sensitive function.
stripos() – Finds the position of the first occurrence of a string inside another string. this is a case-insensitive function.
strripos() – Finds the position of the last occurrence of a string inside another string. this is also a case-insensitive function.
Syntax:
Parameters used:
there are three parameters in the syntax, two-parameters are mandatory and one parameter is optional. These three parameters are mentioned below:
| PARAMETER | DESCRIPTION |
|---|---|
| original_str (Required): | This parameter refers to the original string in which we need to search the occurrence of the required string. |
| search_str (Required): | This parameter refers to the string that we need to search |
| start_pos (optional): | 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.
Technical Details
| Title | DESCRIPTION |
|---|---|
| PHP Version: | 4+ |
| Changelog: | PHP 7.1.0 – The start/beginning parameter can be a negative number |
| Return Value: | Returns the position of the first occurrence of a string inside another string, or FALSE if the string isn’t found. Important Note: String positions start/begin at 0, and not 1. |
Example of PHP strpos Function
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?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 the position of string: " . $position; } else{ return "it is Not Found"; } } // Driver Code $string = "Welcome to my website TutorialScan"; $search = "Scan"; echo Search($search, $string); ?> |
Found at position of string: 30
PHP strpos Function Related Question and Answer:
What is the strpos () function used for?
Answer: The strpos() function used when we need to finds the position of the first occurrence of a string inside another string.
Is Strpos case sensitive?
Answer: This function returns an integer value of the position of the first occurrence of the string. it is the case-sensitive function, which means that it treats upper-case & lower-case characters differently.
What does Strpos return if not found?
Answer: Returns false if the needle was not found. This function may return Boolean false, but may also return a non-Boolean value which evaluates to false.