PHP strpos | Position of Substring in String (Case Sensitive)

PHP strpos is an inbuilt function in PHP. It returns the position of the first occurrence of one string in another. It is very helpful in determining if a string exists in another string. In this article, we will discuss the PHP strpos Function. Also, we will discuss a few examples of using it.

In order to find the last occurrence of a substring in a string, you can use the PHP strrpos Function.

Note: This function is case-sensitive. Therefore, it treats uppercase and lowercase characters differently. If you want to perform a case-insensitive search, use PHP stripos Function.

Syntax

integer strpos ($originalString, $subString, $startPosition = 0);

Parameters

The PHP strpos Function expects three parameters. However, only two parameters are mandatory while the third one is optional. The description of the parameters is as follows:

  • $originalString: The first parameter to the function is the string to search in. Also, it is a mandatory parameter.
  • $subString: The second parameter to the function is the string to search for. It is also a mandatory parameter.
  • $startPosition: The start position parameter refers to the starting point of search. By default, the function searches from the beginning of the original string. It is an optional parameter.

Return Value

The PHP strpos Function returns the integer value of the position of the first occurrence of a substring in the string. Also, it performs the search in a case-sensitive manner. Therefore, upper and lowercase characters are treated as different. However, if the substring does not exist in the string, it returns FALSE.

PHP strpos Function

Examples

Let’s discuss a few examples of using the function.

Example 1: Basic Search

For instance, consider two simple strings.

<?php

    $originalString = 'Bringing Knowledge Together';
    $subString = 'Knowledge';

    //Find Knowledge in the original string
    $position = strpos($originalString, $subString);
?>

In the above example, the substring exists in the original string. The function will return the position of the first occurrence.

OUTPUT:
9

Example 2: Multiple Occurrences

Even if the substring exists multiple times in the original string, the PHP strpos Function returns the position of the first occurrence.

<?php

    $originalString = 'pqr abc, abc, xyz';
    $subString = 'abc';

    //Find Knowledge in the original string
    $position = strpos($originalString, $subString);

?>

In the above example, we search for ‘abc’ in the original string. The substring occurs multiple times in the string. However, the function returns the position of the first occurrence only.

OUTPUT:
4

Example 3: String Not Found

In case the substring is not present in the original string, the function returns false.

<?php

    $originalString = 'Bringing Knowledge Together';
    $Substring = 'Concatly';

    //Find Concatly in the original string
    $partString = strpos($haystack, $needle);
    var_dump($partString);

?>
OUTPUT:
bool(false)

Example 4: Using Start Position

By default, the function searches from the beginning of the string. However, we can modify it by passing the third parameter.

<?php

    $originalString = 'pqr abc, abc, xyz';
    $subString = 'abc';

    //Find Knowledge in the original string
    $position = strpos($originalString, $subString, -10);
    echo $position;

?>

The substring ‘abc’ occurs multiple times in the original string. As we observed in Example 2, the first occurrence of the substring was in position 4. However, on passing the start position as 5, the function begins its search from the 5th position. The output will be as follows:

OUTPUT:
9

Conclusion

In conclusion, we discussed the PHP strpos Function. You can read more about it on PHP Official Documentation. Additionally, you can learn about more PHP String Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *