Instance
Find the location where "PHP" first appears in the string:
<?phpecho Strpos ("I love php, I love php too!", "PHP"); >
Definition and usage
The Strpos () F function finds the position of the first occurrence of a string in another string (case-sensitive).
Note: the Strpos () function is case-sensitive.
Note: This function is binary safe.
Related functions:
Strrpos ()-Finds the last occurrence of a string in another string (case-sensitive)
Stripos ()-finds the position of the first occurrence of a string in another string (case insensitive)
Strripos ()-Finds the last occurrence of a string in another string (case insensitive)
Grammar
Strpos (String,find,start)
parameters |
description |
string |
required. Specifies the string to be searched. |
find |
required. Specifies the character to find. |
start |
optional. Specify where to start the search. |
Technical details
return value: |
Returns the position of the first occurrence of a string in another string, or FALSE if no string is found. Note: The string position starts at 0, not from 1. |
PHP version: |
4 + |
The return value of the Strpos () function returns False if not found, and returns 0 if the substring appears at first. In order to distinguish between 0 and false returned, you must use the equal operator = = = or!==.
<?php $mystring = ' ABCDE '; $findme = ' ab '; $pos = Strpos ($mystring, $findme); For //Note our use of = = =. Simply = = would not work as expected //because the position of ' AB ' is the 0th (first) character. / /Here is used constant equals = = =, if use = = words can not get the expected result of the * //Because the string ab is the No. 0 character starting from the if ($pos = = = False) 11 { echo "The string ' $findme ' is not found in the string ' $mystring '"; + else , { echo "the string ' $findme ' was found in the string ' $mystring '" ; echo "and exists at position $pos"; ?>
Program output:
The string ' ab ' is found in the string ' ABCDE ' and exists at position 0