PHP mb_stripos() Function
The mb_stripos() function is an inbuilt case-insensitive function that finds the first occurrence of a substring in the string.
Syntax:
int|false mb_stripos(
string $haystack,
string $needle,
int $offset ,
?string $encoding
);
Parameters: This function accepts 4 parameters that are described below.
- $haystack: This parameter is the main string parameter where we check the string.
- $needle: This parameter is searching string parameter in the main string.
- $offset: This parameter is an optional parameter. It is used for defining where you are to start searching string in the $haystack parameter.
- $encoding: This parameter is also optional. It is used for defining an encoding by using the mb_internal_encoding() function.
Return Values: This function returns the integer of the first occurrence of $needle in the $haystack parameter. If $needle is not found in the $haystack parameter, it will return “false”.
Program 1: The following code demonstrates the mb_stripos() function.
PHP
<?php $string = "Hello World" ; $sub = "WORLD" ; $result = mb_stripos( $string , $sub ); if ( $result !== false) { echo "Substring '$sub' found in '$string'" ; } else { echo "Substring '$sub' not found in '$string'" ; } ?> |
Output
Substring 'WORLD' found in 'Hello World'
Program 2: The following program demonstrates the mb_stripos() function.
PHP
<?php $string = "This is a test string" ; $substring = "ing" ; $position = mb_stripos( $string , $substring ); // Output the position echo $position ; ?> |
Output
18
Program 3: The following program demonstrates the mb_stripos() function
PHP
<?php $string = "She sells seashells by the seashore." ; $substring = "S" ; $position = 0; $occurrences = 0; while (( $position = mb_stripos( $string , $substring , $position )) !== false) { $occurrences ++; $position = $position + mb_strlen( $substring ); } echo "The substring '$substring' was " + "found $occurrences times in the string." ; ?> |
Output
The substring 'S' was found 8 times in the string.
Reference: https://www.php.net/manual/en/function.mb-stripos.php
Contact Us