PHP stripos | Position of Substring in String (Case Insensitive)

PHP stripos 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 stripos Function. Also, we will discuss a few examples of using it.

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

Syntax

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

Parameters

The PHP stripos 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 stripos 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-insensitive manner. Therefore, upper and lowercase characters are treated as same. However, if the substring does not exist in the string, it returns FALSE.

PHP stripos 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 = stripos($originalString, $subString);
?>

In the above example, the substring exists in the original string. The function will return the position of the first occurrence. Also, notice it searches in a case-insensitive manner.

OUTPUT:
9

Example 2: Multiple Occurrences

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

<?php

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

    //Find Knowledge in the original string
    $position = stripos($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

You can refer to PHP strpos Function for more detailed examples.

Conclusion

In conclusion, we discussed the PHP stripos 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 *