|
| HOW TO USE THE strpos() FUNCTION IN PHP
The strpos() function finds the first occurrence of a string inside another string. The function returns the position of this first occurrence. If the string is not found, the function returns false. The strpos() function is case sensitive.
The general format is:
strpos(string to search, string to find, starting position);
string to search - The string to perform the search on.
string to find - The string to find.
starting position - Integer. Optional. Indicates position to start search from. If not set, position starts from beginning of string by default.
Example Without The Optional Start Position
$string_to_search="My name is Wendy Moss";
$string_to_find="n";
$position=strpos($string_to_search,$string_to_find);
echo $position;
The above outputs: 3 as n is first found at position 3 (0 for M, 1 for y, 2 for space and 3 for n).
Example With The Optional Start Position
$string_to_search="My name is Wendy Moss";
$string_to_find="n";
$position=strpos($string_to_search,$string_to_find, 4);
echo $position;
The above outputs: 13 as n is first found at position 13. We told the function to start at position 4 only so the n at position 3 is ignored.
See also:
|
|