PHP strpos() Function: Syntax, Examples
The strpos() function in PHP is a built-in function which is used to find the position of the first occurrence of a substring within another string.
If the substring is found, the function returns the index (i.e. numeric position) of the first occurrence of a substring within a string. If not found, it returns false.
Syntax of strpos() Function in PHP
The general syntax to define strpos() function in PHP is as follows:
strpos(string $haystack, string $needle, int $offset = 0): int|false
The strpos() function has three parameters: $haystack, $needle, and $offset.
- $haystack is a main string where you want to search in.
- $needle specifies the substring you are trying to find inside the haystack (i.e. main string).
- $offset is an integer that represents index (or position) at which the strpos() function starts the search. The default value of $offset is 0, which means the search starts from the beginning of the string.
- If $offset is positive, the strpos() function will start the search at $offset number of characters counted from the beginning of the string.
In PHP 8.0 and later, strpos() function does not support negative $offset values. If you try to use a negative offset, PHP will throw a ValueError. The first and second parameters are required, while the third parameter is an optional.
Return Value
The strpos() function returns the position (as an integer) of the first occurrence of the $needle (substring) in the $haystack (main string). The position returned is zero-based, meaning the first character in the string is at position 0. If the $needle is not found, the function returns false.
Basic Examples of strpos() Function
Example 1: Let’s write a PHP program to search for the position of a substring within a main string using the strpos() function.
<?php
$text = "Learning PHP is fun and useful!";
$substring = "PHP";
$position = strpos($text, $substring);
if($position !== false) {
echo "The substring '$substring' was found at position: $position";
} else {
echo "The substring '$substring' was not found in the string.";
}
?>
Output: The substring 'PHP' was found at position: 9
In this example, we created a string named $text and assigned a value to it. Then, we defined a substring named $substring and assigned the value “PHP” to it. After that, we called the strpos() function to search for the position of the substring “PHP” within the main string.
We used !== (strict not equal operator) in the if-else statement to check the result because the position 0 is a valid index and 0 !== false is true, since their types are different (int vs bool). In a loose comparison, 0 is treated as false so it can give incorrect result in some cases. Using strict comparison ensures accurate results.
Example 2: Let’s write a PHP program in which we will search for the position of a substring within a main string, but it would not found.
<?php
$text = "Welcome to the coding world!";
$substring = "Python";
$position = strpos($text, $substring);
if ($position !== false) {
echo "'$substring' found at position: $position";
} else {
echo "'$substring' not found in the text.";
}
?>
Output: 'Python' not found in the text.
Case Sensitivity in PHP strpos() Function
Example 3: Let’s write a PHP program in which we will see the strpos() function is case-sensitive or not.
<?php
$text = "php is powerful programming!";
$substring = "PHP";
$position = strpos($text, $substring);
if ($position !== false) {
echo "'$substring' found at position: $position";
} else {
echo "'$substring' not found due to case sensitivity.";
}
?>
Output: 'PHP' not found due to case sensitivity.
The above example shows that the strpos() function is a case-sensitive function in PHP. To perform a case-insensitive search, use stripos() function provided by PHP. We will learn about this function in the next tutorial.
PHP strpos() Function with Offset Argument
Example 4: Let’s write a PHP program in which we will use the strpos() function to search for the specific position of substring in the string start from the first.
<?php
$text = "PHP is a powerful programming language.";
$substring = "p";
// Find the first 'p'.
$first = strpos($text, $substring);
// Find the second 'p', starting after the first.
$second = strpos($text, $substring, $first + 1);
echo "First 'p' at: $first \n";
echo "Second 'p' at: $second";
?>
Output: First 'p' at: 9 Second 'p' at: 18
In this example, the strpos() function returns the index of the second occurrence of the substring ‘p’ which is 18 because it starts the search at the first occurrence of substring ‘p’.
Find All Occurrences of Substring using Loop
Example 5: Let’s write a PHP program to find all positions of a substring appearing in a string.
<?php
$text = "PHP is a powerful programming language. Therefore, many developers love using PHP for web development.";
$substring = "PHP";
$strlen = strlen($substring);
$offset = 0;
while (($position = strpos($text, $substring, $offset)) !== false) {
echo "'$substring' found at position: $position \n";
$offset = $position + $strlen;
}
?>
Output: 'PHP' found at position: 0 'PHP' found at position: 78
In this example, we defined a string named $text and assigned a value to it. Then, we defined a substring named $substring and assigned the value “PHP” to it. We use the strpos() function to search for this substring inside the $text string.
The statement $strlen = strlen($substring); calculates the length of the substring (“PHP”), which is 3. This value is used to move the offset forward after each match, so we don’t repeatedly find the same occurrence.
The statement $offset = 0; initializes the offset, which determines the position in the string from where the search should begin. It starts at position 0, i.e., the beginning of the string.
The line ($position = strpos($text, $substring, $offset)) inside the while loop searches for the next occurrence of “PHP” in the main string, starting from the current value of $offset. If a match is found, it returns the position (index) of the occurrence. If not, it returns false, and the loop ends.
Inside the while loop, the line $offset = $position + $strlen; updates the offset so that the next search begins immediately after the current match. This prevents the loop from getting stuck on the same occurrence repeatedly.
Conclusion
In this tutorial, you learned how to use the strpos() function in PHP to find the first occurrence of a substring within a string. If you want to find the last occurrence of a substring, you can use the strrpos() function instead.