The strpos () function in PHP is one of the most used string-type data in processing. It allows you to determine the position starting from which the desired substring enters the source line. Moreover, to obtain an adequate result, a number of subtleties should be taken into account.
Input and Output Parameters
In general, the syntax of the PHP strpos () function is as follows:
mixed strpos (string $string , mixed $substring[, int $offset = 0 ] )
Two required arguments and one optional are accepted at the input:
- The initial string is $ string, in which the search will be performed.
- The substring to find is $ substring. This parameter is of type mixed. This means that in addition to the string data type, the function can accept any other. In this case, the argument will be converted to an integer and processed as a character code.
- An optional parameter that defines the offset is $ offset. By default, the search is performed from the very beginning of the string $ string, and the offset is 0. If you define a negative offset, it will be counted from the end of the string.
The PHP strpos () function returns the position at which $ substring enters $ string. If there are several such occurrences, only the position of the first one will be returned.
If there are no matches, the output will be boolean false.
Features of work
When searching for a substring position, it is important to consider that the numbering of characters starts from zero. Therefore, 0 is one of the correct return values.
With a loose comparison, 0 is converted to a logical type, turns to false, and can be interpreted as the absence of occurrences. Therefore, it is very important to use strict equality (===), taking into account the type of compared quantities.
In addition, strpos () is an example of a PHP case-sensitive function.
Examples of using
Example 1. Simple entry. Find the position at which the substring key enters the monkeys string.
<?php $string = 'monkeys'; $substring = 'key'; $result = strpos($string, $substring);
Example 2. The lack of occurrences. If you change the search string to KEY, the PHP strpos () function will not detect matches, as it is case sensitive. The result is a boolean value of false.
<?php $string = 'monkeys'; $substring = 'KEY'; $result = strpos($string, $substring); echo($result === false); ?>
Example 3. Entering at zero position. It is important to use strict equality, checking the result of the function, otherwise you can get an incorrect result.
<?php $string = 'lifehack'; $substring = 'life'; $result = strpos($string, $substring); if (!$result) { echo ' '; } if ($result == false) { echo ' '; } if ($result === false) { echo ' '; } ?>
The first two checks will determine the absence of occurrences, despite the fact that the substring life is included in the original string lifehack.
Example 4. The offset of the starting position using the $ offset parameter.
<?php $string = 'love-and-love'; $substring = 'love'; $result1 = strpos($string, $substring); $result2 = strpos($string, $substring, 3); echo $result1;
After setting the offset to 3, the search starts with the character "e".
Similar functions
To determine the position of a substring in PHP, there are other functions that differ from strpos ():
- stripos () - is not case sensitive;
- strrpos () - defines the last occurrence;
- strripos () - Searches for the last occurrence and is not case sensitive.