PHP string functions use the example series-find the sub-string position functions strpos/stripos and strrpos/strripos

Source: Internet
Author: User
1. strposintstrpos (string $ haystack, mixed $ needle [, int $ offset0]) function: find the position of the first occurrence of a string. description: Return & nbsp; needle & nbsp; in & nbsp; the first digit position in haystack & nbsp. And & nbsp; 1. strpos
int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

Function:Locate the first occurrence of a string

Note:?needle? In?haystack? The position of the number that appears for the first time. And? Strrpos ()? Different, before PHP 5, this function can use a complete string?needleAnd the entire string will be used.

Parameters:

haystack

Search for the string.

needle

If?needle? If it is not a string, it will be converted to an integer and considered as the sequential value of the character.

offset

Optional?offset? The parameter can be used to specify the slave?haystack? To start searching. The position of the returned number is relative?haystack.

Return value:Returns the location information with an integer. If no needle is found, strpos () returns a boolean value of FALSE.

Example 1: use =

 

Pay attention to this point:

if(strpos($mystring,$findme)){    //do something;}

This may cause unexpected results, because if the position is 0 (the first character), it is actually found, however, if the result is FALSE, it cannot be processed as expected.

? UseDisplayExample 2: Use! =

 

Example 3: offset

 
II. stripos

The method used by the strpos () function is the same. The only difference is that stripos is case insensitive.

III. strrpos
?int strrpos ( string $haystack , string $needle [, int $offset = 0 ] )

Function:Calculates the position of the last occurrence of the specified string in the target string.

Note:Returns the last numeric position of the needle in the haystack string. Note that in PHP4, needle can only be a single character. if needle is specified as a string, only the first character is used.

Parameters:

haystack

Search for this string.

needle

If?needleIt is not a string, it will be converted to an integer and considered as the sequential value of characters.

offset

You may find any length of the substring in the string. A negative value causes the query to end at the count position starting at the end of the string.

Return value:Returns the position of the needle. If not found, return?FALSE.

Example 1: Check whether the string exists

Like strpos, it is easy to make a mistake between "finding at 0" and "no strings found.

 

Example 2: search by offset

 
IV. strripos

The method used by the strrpos () function is the same. The only difference is that strripos is case insensitive.

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.