PHP find the first occurrence of a string in another string (case-sensitive) function Strpos ()

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.