stripos – PHP String Functions

Syntax :

stripos ( string, search_string, start );

Description :

It’s an inbuilt function of PHP. stripos() function find the position of the first occurrence of a search_string in a string.

Note: The function is case-insensitive.
Note: This function is binary-safe.

Parameter :

[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|left|left|left”]
Name, Required /Optional, Value Type, Description

string, Required, String, Main string to check into.

search_string, Required, String, String to be searched for.

start, OptionalString,  Specifies where to start searching in string.

[/table]


Output :

Returns the of the first occurrence of a search_string inside string. Also note that string positions start at 0, and not 1.

Returns FALSE if the search_string was not found.


ChangeLog :

[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
PHP Version, Description
7.1.0, Support for negative length has been added.

[/table]


Related articles :  strpos(), strrpos(), strripos(), stristr(), str_ireplace(), substr(), mb_stripos().


stripos() – PHP Functions Example 1 :
<?php 
$mystring = 'tutor';
$findme = 't';
$pos = stripos($mystring, $findme);

// Note our use of ===. Simply == would not work as expected
// because the position of 't' was the 0th (first) character.
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
?>

Output of above code in the browser is as below:

The string ‘t’ was found in the string ‘tutor’ and exists at position 0

stripos() – PHP Functions Example 2 : Using length parameter.
<?php
// We can search for the character, ignoring anything before the offset
$newstring = 'hello from tutor. hi all. Hi all.';
$pos = stripos($newstring, 'H', 1); 
echo $pos;
?>

Output of above code in the browser is as below:

18

stripos() – PHP Functions Example 3 :   Returns FALSE if the search_string was not found.
<?php
// We can search for the character, ignoring anything before the offset
$newstring = 'hello from tutor. hi all.';
$pos = stripos($newstring, 'b', 1); 
var_dump($pos);
?>

Output of above code is :

bool(false)

You may also like...