stripos
FIND the position of the FIRST occurrence of a of a substring in a STRING.
This function is case-insensitive
If $needle is not a STRING, 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, and relying on it is highly discouraged.
Depending on the intended behavior, the $needle should either be explicitly cast to STRING, or an explicit call to the function chr should be performed.
This function returns the portion of STRING, or FALSE if $needle is not found.
If $offset < 0 the search will start this number of characters counted from the end of the STRING.
Remember that STRING positions start at 0, and not 1.
Watch...
This function may return FALSE, but may also return a non-Boolean value which evaluates to FALSE.
Use the === operator for testing the return value of this function.
<?php
int stripos ( str $haystack , mix $needle [, int $offset = 0 ] )
where,
$haystack = The input STRING to search in
$needle = The needle to be found
$offset = The position where the search will start
?>
$haystack
The STRING to search in.
$needle
The parameter to be found.
$offset
The position where the search begin.
EXERCISE
<?php
$str_hp = 'Estou procurando uma agulha
em um palheiro! (UM TRABALHO DIFÍCIL)';
$str_np = 'um';
$int_ap = stripos($str_hp, $str_np);
echo 'A primeira ocorrência de [' . $str_np . ']
<br><br> em, "' . $str_hp . '"
<br><br> é verificada na posição [' . $int_ap . ']';
echo '<br><br><br><br>';
$str_he = "I'm looking for a needle
in a haystack! (A HARD WORK)";
$str_ne = 'A';
$int_ae = stripos($str_he, $str_ne);
echo 'The first occurrence of [' . $str_ne . ']
<br><br> in, "' . $str_he . '"
<br><br> is found at position [' . $int_ae . ']';
?>
RESULT
PORTUGUESE
A primeira ocorrência de um
em, "Estou procurando uma agulha em um palheiro! (UM TRABALHO DIFÍCIL)"
é verificada na posição 17
ENGLISH
The first occurrence of A
in, "I'm looking for a needle in a haystack! (A HARD WORK)"
is found at position 16
EXERCISE
<?php
$str_hpx2 = "I'm looking for a needle
in a haystack! (A HARD WORK)";
$str_np2 = 'haystack';
$off_p2 = -50;
$int_ap22 = stripos($str_hpx2, $str_np2, $off_p2);
echo $str_hpx2;
echo '<br><br><br>The first occurrence of ' . $str_np2
. ', is found at ' . $int_ap22 . ', in relation to the position '
. $off_p2 . '<br><br>' . $str_hpx2 . '<br><br>';
?>
RESULT
I'm looking for a needle in a haystack! (A HARD WORK)
The first occurrence of haystack, is found at position 32, in relation to the position -50
I'm looking for a needle in a haystack! (A HARD WORK)
EXERCISE
<?php
$str_hpx3 = "I'm looking for a needle
in a haystack! (A HARD WORK)";
$str_np3 = 'HAYSTACK';
$off_p3 = -50;
$int_ap32 = stripos($str_hpx3, $str_np3, $off_p3);
echo $str_hpx3;
echo '<br><br><br>The first occurrence of ' . $str_np3
. ', is found at ' . $int_ap32 . ', in relation to the position '
. $off_p3 . '<br><br>' . $str_hpx3 . '<br><br>';
?>
RESULT
I'm looking for a needle in a haystack! (A HARD WORK)
The first occurrence of HAYSTACK, is found at position 32, in relation to the position -50
I'm looking for a needle in a haystack! (A HARD WORK)
EXERCISE
<?php
var_dump(stripos("test string", "TEST"));
echo '<br><br>';
var_dump(stripos("test string", "strIng"));
echo '<br><br>';
var_dump(stripos("test string", "stRin"));
echo '<br><br>';
var_dump(stripos("test string", "t S"));
echo '<br><br>';
var_dump(stripos("test string", "G"));
echo '<br><br>';
var_dump(stripos("te".chr(0)."st", chr(0)));
echo '<br><br>';
var_dump(stripos("tEst", "test"));
echo '<br><br>';
var_dump(stripos("teSt", "test"));
echo '<br><br>';
var_dump(stripos("", ""));
echo '<br><br>';
var_dump(stripos("a", ""));
echo '<br><br>';
var_dump(stripos("", "a"));
echo '<br><br>';
var_dump(stripos("a", " "));
echo '<br><br>';
var_dump(stripos("a", "a"));
echo '<br><br>';
var_dump(stripos("", 1));
echo '<br><br>';
var_dump(stripos("", false));
echo '<br><br>';
var_dump(stripos("", true));
echo '<br><br>';
var_dump(stripos("a", 1));
echo '<br><br>';
var_dump(stripos("a", false));
echo '<br><br>';
var_dump(stripos("a", true));
echo '<br><br>';
var_dump(stripos("1", 1));
echo '<br><br>';
var_dump(stripos("0", false));
echo '<br><br>';
var_dump(stripos("1", true));
echo '<br><br>';
var_dump(stripos("\\\\a", "\\a"));
?>
EXERCISE
<?php
/* Test stripos() function by passing
* single quoted strings to 'haystack'
* & 'needle' arguments */
echo "Testing stripos() function:
with single quoted strings.<br>";
$haystack = 'Hello,\t\n\0\n
$&!#%\o,()*+-./:;<=>?@hello123456he \x234 \101 ';
$needle = [
//regular strings
'l',
'L',
'HELLO',
'hEllo',
//escape characters
'\t',
'\T',
' ',
'\n',
'\N',
'
', //new line
//nulls
'\0',
NULL,
null,
//boolean false
FALSE,
false,
//empty string
'',
//special chars
' ',
'$',
' $',
'&',
'!#',
'%\o',
'\o,',
'()',
'*+',
'+',
'-',
'.',
'.;',
'.;',
':;',
';',
'<=>',
'>',
'=>',
'?',
'@',
'@hEllo',
'12345',
//decimal numeric string
'\x23',
//hexadecimal numeric string
'#',
//hexadecimal numeric string
'\101',
//octal numeric string
'A',
'456HEE',
//numerics + chars
42,
//needle as int(ASCII value of '*')
$haystack
//haystack as needle
];
/* loop through to get the position
of the needle in haystack string */
$count = 1;
for($index=0; $index<count($needle); $index++) {
echo "<br><br>Iteration: $count<br>";
var_dump( stripos($haystack, $needle[$index]) );
var_dump( stripos($haystack, $needle[$index], $index) );
$count++;
}
?>
EXERCISE
<?php
/* Test stripos() function by passing
* multi-line heredoc string for
* haystack and
* with various needles & offsets
*/
echo "Testing stripos() function: with heredoc strings.<br>";
echo "<br><br>With heredoc string containing multi lines:<br>";
$multi_line_str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
var_dump( stripos($multi_line_str, "ing", 0) );
echo '<br><br>';
var_dump( stripos($multi_line_str, "ing", 15) );
echo '<br><br>';
var_dump( stripos($multi_line_str, "ing", 22) );
echo '<br><br>';
var_dump( stripos($multi_line_str, "") );
echo '<br><br>';
var_dump( stripos($multi_line_str, " ") );
?>
EXERCISE
<?php
/* Test stripos() function by passing
* heredoc string containing
* special chars for haystack
* and with various needles & offsets
*/
echo "Testing stripos() function: with heredoc strings.<br>";
echo "<br><br>With heredoc string containing special chars:<br>";
$special_chars_str = <<<EOD
Ex'ple of h'doc st'g, contains
$#%^*&*_("_")!#@@!$#$^^&$*(special)
chars.
EOD;
var_dump( stripos($special_chars_str, "Ex'ple", 0) );
echo '<br><br>';
var_dump( stripos($special_chars_str, "!@@!", 23) );
echo '<br><br>';
var_dump( stripos($special_chars_str, '_') );
echo '<br><br>';
var_dump( stripos($special_chars_str, '("_")') );
echo '<br><br>';
var_dump( stripos($special_chars_str, "$*") );
echo '<br><br>';
var_dump( stripos($special_chars_str, "$*", 10) );
echo '<br><br>';
var_dump( stripos($special_chars_str, "(special)") );
?>
EXERCISE
<?php
/* Test stripos() function by passing
* heredoc string containing
* escape chars for haystack
* and with various needles & offsets
*/
echo "Testing stripos() function:
with heredoc strings.<br>";
echo "<br><br>With heredoc string
containing escape characters:<br>";
$control_char_str = <<<EOD
Hello, World\n
Hello\tWorld
EOD;
var_dump( stripos($control_char_str, "\n") );
echo '<br><br>';
var_dump( stripos($control_char_str, "\t") );
echo '<br><br>';
var_dump( stripos($control_char_str, "\n", 12) );
echo '<br><br>';
var_dump( stripos($control_char_str, "\t", 15) );
?>
EXERCISE
<?php
/* Test stripos() function by passing
* heredoc string containing quotes
* for haystack
* and with various needles & offsets
*/
echo "Testing stripos() function:
with heredoc strings.<br>";
echo "<br><br>With heredoc string
containing quote & slash chars:<br>";
$quote_char_str = <<<EOD
it's bright,but i cann't see it.
"things in double quote"
'things in single quote'
this\line is /with\slashs
EOD;
var_dump( stripos($quote_char_str, "line") );
echo '<br><br>';
var_dump( stripos($quote_char_str, 'things') );
echo '<br><br>';
var_dump( stripos($quote_char_str, 'things', 0) );
echo '<br><br>';
var_dump( stripos($quote_char_str, "things", 20) );
?>
EXERCISE
<?php
/* Test stripos() function by passing
* empty heredoc string for haystack
* and with various needles & offsets
*/
echo "Testing stripos() function: with heredoc strings.<br>";
echo "<br><br>With empty heredoc string:<br>";
$empty_string = <<<EOD
EOD;
var_dump( stripos($empty_string, "") );
try {
stripos($empty_string, "", 1);
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
var_dump( stripos($empty_string, FALSE) );
echo '<br><br>';
var_dump( stripos($empty_string, NULL) );
?>
EXERCISE
<?php
/* Test stripos() function with
* strings containing repetitive chars
* for haystak
* and with various needles & offsets
*/
echo "Testing stripos() function:
strings repetitive chars.<br>";
$haystack = "aBAbaBAbaBabAbAbaBa";
$needles = [
"aba",
"aBA",
"ABA",
"Aba",
"BAb",
"bab",
"bAb",
"BAB"
];
/* loop through to consider various
offsets in getting the position
of the needle in haystack string */
$count = 1;
for($index = 0; $index < count($needles); $index++) {
echo "<br><br>Iteration: $count<br>";
for($offset = 0; $offset <= strlen($haystack); $offset++ ) {
var_dump( stripos($haystack, $needles[$index], $offset) );
}
$count++;
}
?>
EXERCISE
<?php
/* Test stripos() function with
* unexpected inputs for 'needle' and
* an expected type of input for
* 'haystack' argument
*/
echo "Testing stripos() function with
unexpected values for needle.<br>";
//get an unset variable
$unset_var = 'string_val';
unset($unset_var);
//defining a class
class sample {
public function __toString() {
return "object";
}
}
//getting the resource
$file_handle = fopen(__FILE__, "r");
$haystack = "string 0 1 2 -2 10.5 -10.5
10.5e10 10.6E-10 .5
array true false object \"\" null Resource";
// array with different values
$needles = array (
// integer values
0,
1,
12345,
-2345,
// float values
10.5,
-10.5,
10.1234567e10,
10.7654321E-10,
.5,
// array values
array(),
array(0),
array(1),
array(1, 2),
array('color' => 'red', 'item' => 'pen'),
// boolean values
true,
false,
TRUE,
FALSE,
// objects
new sample(),
// empty string
"",
'',
// null values
NULL,
null,
// resource
$file_handle,
// undefined variable
@$undefined_var,
// unset variable
@$unset_var
);
// loop through each element of the
// 'needle' array to check the working of stripos()
$counter = 1;
for($index = 0; $index < count($needles); $index ++) {
echo "<br><br>Iteration: $counter<br>";
try {
var_dump( stripos($haystack, $needles[$index]) );
} catch (TypeError $e) {
echo $e->getMessage(), "<br>";
}
$counter ++;
}
fclose($file_handle);
//closing the file handle
?>
EXERCISE
<?php
/* Test stripos() function with
* unexpected inputs for 'haystack'
* and 'needle' arguments */
echo "Testing stripos() function with
unexpected values for
haystack and needle.<br>";
// get an unset variable
$unset_var = 'string_val';
unset($unset_var);
// defining a class
class sample {
public function __toString() {
return "object";
}
}
//getting the resource
$file_handle = fopen(__FILE__, "r");
// array with different values
$values = array (
// integer values
0,
1,
12345,
-2345,
// float values
10.5,
-10.5,
10.5e10,
10.6E-10,
.5,
// array values
array(),
array(0),
array(1),
array(1, 2),
array('color' => 'red', 'item' => 'pen'),
// boolean values
true,
false,
TRUE,
FALSE,
// objects
new sample(),
// empty string
"",
'',
// null values
NULL,
null,
// resource
$file_handle,
// undefined variable
@$undefined_var,
// unset variable
@$unset_var
);
// loop through each element of the
// array and check the working of stripos()
$counter = 1;
for($index = 0; $index < count($values); $index ++) {
echo "<br><br>Iteration: $counter<br>";
$haystack = $values[$index];
try {
var_dump( stripos($values[$index], $values[$index]) );
} catch (Error $e) {
echo get_class($e) . ": " . $e->getMessage(), "<br>";
}
try {
var_dump( stripos($values[$index], $values[$index], 1) );
} catch (Error $e) {
echo get_class($e) . ": " . $e->getMessage(), "<br>";
}
$counter ++;
}
?>
EXERCISE
<?php
/* Test stripos() function with null
* terminated strings for 'haystack'
* argument in order to check the binary safe
*/
echo "Test stripos() function: binary safe.<br>";
$haystacks = array(
"Hello".chr(0)."World",
chr(0)."Hello World",
"Hello World".chr(0),
chr(0).chr(0).chr(0),
"Hello\0world",
"\0Hello",
"Hello\0"
);
for($index = 0; $index < count($haystacks); $index++ ) {
var_dump( stripos($haystacks[$index], "\0") );
echo '<br><br>';
var_dump( stripos($haystacks[$index], "\0", $index) );
}
?>
EXERCISE
<?php
/* Test stripos() function with null
* terminated strings for 'needle'
* argument in order to check binary safe
*/
echo "Test stripos() function: binary safe.<br>";
$haystack = "\0Hello\0World\0";
$needles = array(
"Hello".chr(0)."World",
chr(0)."Hello World",
"Hello World".chr(0),
chr(0).chr(0).chr(0),
"Hello\0world",
"\0Hello",
"Hello\0"
);
for($index = 0; $index < count($needles); $index++ ) {
var_dump( stripos($haystack, $needles[$index]) );
echo '<br><br>';
var_dump( stripos($haystack, $needles[$index], $index) );
}
?>
EXERCISE
<?php
/* Test stripos() function by passing
* double quoted strings for 'haystack'
* & 'needle' arguments */
echo "Testing stripos() function:
with double quoted strings.<br>";
$haystack = "Hello,\t\n\0\n
$&!#%\o,()*+-./:;<=>?@hello123456he \x234 \101 ";
$needle = array(
//regular strings
"l",
"L",
"HELLO",
"hEllo",
//escape characters
"\t",
"\T",
//invalid input
" ",
"\n",
"\N",
//invalid input
"
", //new line
//nulls
"\0",
NULL,
null,
//boolean false
FALSE,
false,
//empty string
"",
//special chars
" ",
"$",
" $",
"&",
"!#",
"%\o",
"\o,",
"()",
"*+",
"+",
"-",
".",
".;",
":;",
";",
"<=>",
">",
"=>",
"?",
"@",
"@hEllo",
"12345",
//decimal numeric string
"\x23",
//hexadecimal numeric string
"#",
//respective ASCII char of \x23
"\101",
//octal numeric string
"A",
//respective ASCII char of \101
"456HEE",
//numerics + chars
$haystack
//haystack as needle
);
/* loop through to get the position
of the needle in haystack string */
$count = 1;
for($index=0; $index<count($needle); $index++) {
echo "<br><br>Iteration: $count<br>";
var_dump( stripos($haystack, $needle[$index]) );
var_dump( stripos($haystack, $needle[$index], $index) );
$count++;
}
?>
EXERCISE
<?php
echo "Testing stripos() function: error conditions.<br>";
echo "<br><br>Offset beyond the end of the string:<br>";
try {
stripos("Hello World", "o", 12);
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
echo "<br><br>Offset before the start of the string:<br>";
try {
stripos("Hello World", "o", -12);
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
?>