strpos
FIND the position of the FIRST occurrence of a of a substring in a STRING.
This function is case-sensitive
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 strpos ( 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 = strpos($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 = strpos($str_he, $str_ne);
echo 'The first occurrence of [' . $str_ne . ']
<br><br> in, "' . $str_he . '"
<br><br> is found at position [' . $int_ae . ']';
?>
EXERCISE
<?php
$str_hpx2 = "I'm looking for a needle
in a haystack! (A HARD WORK)";
$str_hpy2 = "I'm looking for a needle
in a haystack! (A HARD WORK)";
$str_hp2 = "I'm looking for a needle
in a haystack! (A HARD WORK)";
$str_np2 = 'a';
$off_p2 = 18;
$off_o2 = 0;
$int_ap21 = strpos($str_hpx2, $str_np2, $off_o2);
$int_ap22 = strpos($str_hpx2, $str_np2, $off_p2);
echo $str_hpx2;
echo '<br><br><br>The first occurrence of ' .
$str_np2 . ', is found at position ' .
$int_ap21 . ', in relation to the position '
. $off_o2 . '<br><br>' . $str_hpy2 . '<br><br>';
echo '<br><br><br>The first occurrence of ' .
$str_np2 . ', is found at position ' .
$int_ap22 . ', after the position '
. $off_p2 . '<br><br>' . $str_hp2;
?>
EXERCISE
<?php
$str_hpx3 = "I'm looking for a needle
in a haystack! (A HARD WORK)";
$str_hpy3 = "I'm looking for a needle
in a haystack! (A HARD WORK)";
$str_hp3 = "I'm looking for a needle
in a haystack! (A HARD WORK)";
$str_np3 = 'A';
$off_p3 = 18;
$off_o3 = 0;
$int_ap31 = strpos($str_hpx3, $str_np3, $off_o3);
$int_ap32 = strpos($str_hpx3, $str_np3, $off_p3);
echo $str_hpx3;
echo '<br><br><br>The first occurrence of ' . $str_np3
. ', is found at ' . $int_ap31 . ', in relation to the position '
. $off_o3 . '<br><br>' . $str_hpy3 . '<br><br>';
echo '<br><br><br>The first occurrence of ' . $str_np3
. ', is found at ' . $int_ap32 . ', after the position '
. $off_p3 . '<br><br>' . $str_hp3;
?>
EXERCISE
<?php
$str_hpx4 = "I'm looking for a needle
in a haystack! (A HARD WORK)";
$str_np4 = 'haystack';
$off_p4 = -50;
$int_ap42 = strpos($str_hpx4, $str_np4, $off_p4);
echo $str_hpx4;
echo '<br><br><br>The first occurrence of ' . $str_np4
. ', is found at ' . $int_ap42 . ', in relation to the position '
. $off_p4 . '<br><br>' . $str_hpx4 . '<br><br>';
?>
EXERCISE
<?php
$str_hpx5 = "I'm looking for a needle
in a haystack! (A HARD WORK)";
$str_np5 = 'HAYSTACK';
$off_p5 = -50;
$int_ap52 = strpos($str_hpx5, $str_np5, $off_p5);
echo $str_hpx5;
if($int_ap52 === TRUE)
{
echo '<br><br><br>The first occurrence of ' . $str_np5
. ', is found at ' . $int_ap52 . ', in relation to the position '
. $off_p5 . '<br><br>' . $str_hpx5 . '<br><br>';
}
else
{
echo '<br><br>' . $str_np5 . ' is not present in ' .
$str_hpx5 . '<br>';
}
?>
EXERCISE
<?php
echo "Testing basic functionality of strpos():<br><br>";
var_dump( strpos("test string", "test") );
echo '<br><br>';
var_dump( strpos("test string", "string") );
echo '<br><br>';
var_dump( strpos("test string", "strin") );
echo '<br><br>';
var_dump( strpos("test string", "t s") );
echo '<br><br>';
var_dump( strpos("test string", "g") );
echo '<br><br>';
var_dump( strpos("te".chr(0)."st", chr(0)) );
echo '<br><br>';
var_dump( strpos("tEst", "test") );
echo '<br><br>';
var_dump( strpos("teSt", "test") );
echo '<br><br>';
var_dump( strpos("", "") );
echo '<br><br>';
var_dump( strpos("a", "") );
echo '<br><br>';
var_dump( strpos("", "a") );
echo '<br><br>';
var_dump( strpos("\\\\a", "\\a") );
echo "<br><br><br>Testing strpos()
to find various needles and a long string:<br>";
$string =
"Hello world,012033 -3.3445
NULL TRUE FALSE\0 abcd\xxyz \x000 octal\n
abcd$:Hello world";
/* needles in an array to get
the position of needle in $string */
$needles = array(
"Hello world",
"WORLD",
"\0",
"\x00",
"\x000",
"abcd",
"xyz",
"octal",
"-3",
-3,
"-3.344",
-3.344,
NULL,
"NULL",
"0",
0,
TRUE,
"TRUE",
"1",
1,
FALSE,
"FALSE",
" ",
" ",
'b',
'\n',
"\n",
"12",
"12twelve",
$string
);
/* loop through to get the
"needle" position in $string */
for( $i = 0; $i < count($needles); $i++ ) {
echo "<br><br>Position of '$needles[$i]' is => ";
var_dump( strpos($string, $needles[$i]) );
}
echo "<br><br>Testing strpos() with possible variations in offset:<br>";
$offset_values = array (
1,
// offset = 1
"string",
// offset as string, converts to zero
NULL,
// offset as string, converts to zero
"",
// offset as string, converts to zero
"0",
TRUE,
NULL,
FALSE,
"string12",
-10,
// Not found
-15,
// Found
-strlen($string),
);
/* loop through to get the
"needle" position in $string */
for( $i = 0; $i < count( $offset_values ); $i++ ) {
echo "<br><br>Position of 'Hello' with offset '$offset_values[$i]' is => ";
try {
var_dump( strpos($string, "Hello", $offset_values[$i]) );
} catch (TypeError $e) {
echo "<br>", $e->getMessage(), "<br><br>";
}
}
echo "<br><br>Testing miscellaneous input data:<br>";
echo "<br>Passing objects as string and needle:<br>";
/* we get "Recoverable fatal error:
saying Object of class needle could not be
converted to string" by default when
an object is passed instead of string:
The error can be avoided by choosing
the __toString magix method as follows: */
class StringCapable
{
function __toString() {
return "Hello, world";
}
}
$obj_string = new StringCapable;
class needle
{
function __toString() {
return "world";
}
}
$obj_needle = new needle;
var_dump( strpos("$obj_string", "$obj_needle") );
echo "<br><br>Posiibilities with null:<br>";
var_dump( strpos("", NULL) );
echo '<br><br>';
var_dump( strpos(NULL, NULL) );
echo '<br><br>';
var_dump( strpos("a", NULL) );
echo '<br><br>';
var_dump( strpos("/x0", "0") ); // Hexadecimal NUL
echo "<br><br><br>A longer and heredoc string:<br>";
$string = <<<EOD
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz
0123456789
abcdefghijklmnopqrstuvwxyz0123456789
EOD;
var_dump( strpos($string, "abcd") );
echo '<br><br>';
var_dump( strpos($string, "abcd", 72) );
// 72 -> "\n" in the first line
echo '<br><br>';
var_dump( strpos($string, "abcd", 73) );
// 73 -> "abcd" in the second line
echo '<br><br>';
var_dump( strpos($string, "9", (strlen($string)-1)) );
echo "<br><br><br>A heredoc null string:<br>";
$str = <<<EOD
EOD;
var_dump( strpos($str, "\0") );
echo '<br><br>';
var_dump( strpos($str, NULL) );
echo '<br><br>';
var_dump( strpos($str, "0") );
echo "<br><br><br>simple and complex syntax strings:<br>";
$needle = 'world';
/* Simple syntax */
var_dump( strpos("Hello, world", "$needle") );
// works
echo '<br><br>';
var_dump( strpos("Hello, world'S", "$needle'S") );
// works
echo '<br><br>';
var_dump( strpos("Hello, worldS", "$needleS") );
// won't work
echo '<br><br><br>';
/* String with curly braces,
complex syntax */
var_dump( strpos("Hello, worldS", "${needle}S") );
// works
echo '<br><br>';
var_dump( strpos("Hello, worldS", "{$needle}S") );
// works
echo '<br><br>';
echo "<br><br><br>Testing error conditions:<br>";
var_dump( strpos($string, "") );
try {
strpos($string, "test", strlen($string)+1);
// offset > strlen()
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
try {
strpos($string, "test", -strlen($string)-1);
// offset before start
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
var_dump( strpos(NULL, "") );
?>
EXERCISE
<?php
// Integer is handles as an
// octal representation,
// so nothing to match
var_dump(strpos("foo 11", 11));
echo '<br><br>';
var_dump(strpos("foo bar", 111));
echo '<br><br>';
var_dump(strpos("foo 11", "11"));
?>
EXERCISE
<?php
$string = chr(0).chr(128).chr(129).
chr(234).chr(235).chr(254).chr(255);
$stringAsHex = bin2hex($string);
echo "Positions of some chars in
the string '$stringAsHex' are as follows:<br><br>";
echo bin2hex( chr(128) ) ." => ";
var_dump( strpos($string, chr(128)) );
echo '<br><br>';
echo bin2hex( chr(255) ) ." => ";
var_dump( strpos($string, chr(255), 3) );
echo '<br><br>';
echo bin2hex( chr(256) ) ." => ";
var_dump( strpos($string, chr(256)) );
?>