PHP

 Home  Computers & Internet  Web Programming PHP
What is PHP?
Echo
Comments
Variables
Constants
Data Types
number_format()
Character Strings
Mathematical Operators
Comparison Operators
Logical Operators
Joining Strings
explode()
implode()
strtolower()
strtoupper()
strlen()
ucfirst()
ucwords()
strrev()
str_replace()
str_repeat()
trim()
strip_tags()
addslashes()
stripslashes()
strpos()
strrpos()
nl2br()
isset()
unset()
empty()
POST
GET
If Statements
If Else Statements
Elseif Statements
Switch Statements
For Loops
While Loops
Do While Loops
Foreach Loops
File Create
File Open
File Read
File Write
File Delete
fgets()
file_get_contents()
Date & Time
$_SERVER
Sessions
Cookies
Arrays

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:

strrpos() function


Home | Privacy Policy | Terms Of Use | Contact Us