PHP stripos | How to used PHP stripos and How it works?

Summary: In this article, you will learn how to use PHP stripos to find the position of the first occurrence of a case-insensitive substring in a string. Let’s understand the PHP stripos() function with an example.

Definition and Usage PHP stripos() Function

  • The PHP stripos() is an in-build function that is used to search or find the numeric position of the first occurrence of a string inside another string.
  • The stripos() function is case-insensitive.
  • This function is binary-safe.
.
Note: Version:- PHP 5, PHP 7, PHP 8
Related functions:
  • strripos() – This strripos() function is applied in PHP to finds the position of the last occurrence of a string inside another string. it’s a case-insensitive
  • strpos() – This strpos() function is applied in PHP to finds the position of the first(1st) occurrence of a string inside another string. it’s a case-sensitive
  • strrpos() – This strrpos() function is applied in PHP to finds the position of the last occurrence of a string inside another string. it’s a case-sensitive

Syntax

stripos ( string $haystack , string $needle , int $offset = 0 ) : int|false

Find the numeric position of the first(1st) occurrence of a needle in the haystack string.
Unlike the strpos(), stripos() is case-insensitive.

Parameter Values
Parameter Value Description
haystack (Required) The string to search in.
needle (Required) the needle may be a string of one or more than one character.
Prior to PHP 8.0.0, if the needle is not a string, furthermore, it is converted to an integer and applied as the ordinal value of a character. This behavior is deprecated as of PHP 7.3.0, & relying on it is highly discouraged. it is Depending on the intended behavior, the needle should either be explicitly cast to a string, or an explicit call to chr() should be performed.
offset (Optional) If specified, the search will start or began this number of characters counted from the beginning or starting of the string. If the offset is negative(-ve), the search will start or began this number of characters counted from the end of the string.

Return Values

Returns the position of where the needle exists relative to the beginning or start of the haystack string (independent of offset). Important note that string positions start or begin at 0, and not 1.
Returns false if the needle wasn’t found.

Example of PHP stripos() Function

Example 1:
Output: The string ‘b’ was not found in the string ‘xyz’ We found ‘b’ in ‘ABC’ at position 1
Example 2:
The output of the above code segment: 11

Conclusion

In this article, you have learned how to use PHP stripos Function to find the position of the first occurrence of a case-insensitive substring in a string. I hope you will enjoy it!. If have any problem, contact me on info@tutorialscan.com.