[PHP source Reading]strpos, strstr and Stripos, stristr functions, strposstristr_php Tutorial

Source: Internet
Author: User

[PHP Source Read]strpos, strstr and Stripos, stristr functions, Strposstristr


Strpos

Mixed Strpos string $haystack Mixed $needle $offset = 0])

If offset is specified, the lookup starts at the position of offset. Offset cannot be a negative number.

Returns the location of the first occurrence of needle in haystack. Returns False if needle is not found in the haystack.

Needle, if needle is not a string, it is converted to an integer value and assigned the ASCII character of the numeric value. Take a look at the example below.

Example

$str = "Hello"; $pos Strpos ($str, 111); // the ASCII value of 111 is O, so $pos = 4

Strpos Core Source

if(z_type_p (needle) = =is_string) {     if(!z_strlen_p (needle)) {Php_error_docref (NULL tsrmls_cc, e_warning,"Empty Needle");     Return_false; }     //Call the PHP_MEMNSTR function to find needleFound = php_memnstr (haystack +offset, z_strval_p (needle), z_strlen_p (needle), Haystack+Haystack_len); } Else {          //if it is not a string, it is converted to a number and assigned to the ASCII character of the number.           if(Php_needle_char (needle, Needle_char tsrmls_cc)! =SUCCESS)          {return_false; }          //Set End characterneedle_char[1] =0; Found= PHP_MEMNSTR (haystack +offset, Needle_char,1, Haystack+Haystack_len);
}}

One thing to note is that if needle is not a string, the Php_needle_char function is called to convert the needle to an integer number and to its ASCII value.

Find function

The function finally returns the found, and thephp_memnstr function implements the method of finding. So go ahead and see what the PHP_MEMNSTR function does:

#define Php_memnstr zend_memnstr

PHP_MEMNSTR is a macro definition of function zend_memnstr , see the ZEND_MEMNSTR function as follows:

StaticInlineChar*Zend_memnstr (Char*haystack,Char*needle,intNeedle_len,Char*end) {    Char*p =haystack; CharNE = needle[needle_len-1]; if(Needle_len = =1) {        return(Char*) MEMCHR (P, *needle, (end-p)); }    if(Needle_len > end-haystack) {        returnNULL; }    //first optimization, find only end-needle_len timesEnd-=Needle_len;  while(P <=end) {        //second optimization, determine whether the beginning and end of the string is the same as the whole string        if(P = (Char*) MEMCHR (P, *needle, (end-p+1))) && ne = = p[needle_len-1]) {            if(!MEMCMP (needle, p, needle_len-1)) {                returnp; }        }        if(p = =NULL) {            returnNULL; } P++; }    returnNULL;}

The first optimization, because (char *) MEMCHR (P, *needle, (end-p+1) is found in End-needle_len + 1 (that is, haystack_len+1), if P is empty, the first character of Needle is never seen in P.

Strstr

string strstr string $haystack Mixed $needle $before _needle false ] )

Returns the position of the first occurrence of needle in haystack to the end of the string.

This function is case-sensitive.

Returns False if the needle does not exist in the haystack.

If Before_needle is true, returns the string needle the first occurrence of the haystack in haystack.

STRSTR Core Source

if (found) {        //  calculates the position of found        found_offset = found- haystack;         if (part) {            1);         Else {            1);        }    }

The first half of the STRSTR function is similar to Strpos, except that the STRSTR function needs to return the haystack part of the string after locating the position. The part variable is the before_needle variable that is passed when the STRSTR function is called.

Stripos

Mixed Stripos string $haystack string $needle $offset = 0])

Case-insensitive Strpos. The implementation is similar to the following, mainly by using a copy and then converting the string you want to compare to lowercase characters before you find it.

Stristr

string Stristr string $haystack Mixed $needle $before _needle false ] )

Case-insensitive strstr.

Core source

//Copy a haystackHaystack_dup =estrndup (haystack, haystack_len); if(z_type_p (needle) = =is_string) {        Char*Orig_needle; if(!z_strlen_p (needle)) {Php_error_docref (NULL tsrmls_cc, e_warning,"Empty Needle");            Efree (Haystack_dup);        Return_false; } orig_needle=estrndup (z_strval_p (needle), z_strlen_p (needle)); //Call the PHP_STRISTR function to find the value of the Orig_needle. Found =php_stristr (Haystack_dup, Orig_needle, Haystack_len, Z_strlen_p (needle));    Efree (Orig_needle); } Else {        if(Php_needle_char (needle, Needle_char tsrmls_cc)! =SUCCESS)            {Efree (haystack_dup);        Return_false; } needle_char[1] =0; Found= Php_stristr (Haystack_dup, Needle_char, Haystack_len,1); }    if(found) {Found_offset= Found-Haystack_dup; if(part) {Retval_stringl (haystack, Found_offset,1); } Else{Retval_stringl (haystack+ Found_offset, Haystack_len-found_offset,1); }    } Else{retval_false; }        //Releasing VariablesEfree (Haystack_dup);

As you can see, found is obtained from Php_stristr and continues to look at the PHP_STRISTR function:

Char *php_stristr (charChar *T, size_t S_len, size_t t_len) {    Php_strtolower (s, s_ Len);    Php_strtolower (t, T_len);     return Php_memnstr (S, T, T_len, S + S_len);}

The function is to turn the string into lowercase and call the PHP_MENNSTR function to find where needle first appeared in haystack.

Summarize

Because Strpos/stripos returns a position, the position is calculated from 0, so it is more appropriate to determine if the lookup failed with = = = FALSE .

Read the source code of PHP is very much, on the one hand you can know the specific implementation of a function of how the principle, on the other hand can learn some programming optimization program.

To this end of this article, if there are any questions or suggestions, you can communicate a lot, original articles, writing limited, Caishuxueqian, if there is not in the text, million hope to inform.

If this article is helpful to you, look at the next recommendation, thank you ^_^

http://www.bkjia.com/PHPjc/1125523.html www.bkjia.com true http://www.bkjia.com/PHPjc/1125523.html techarticle [php Source Read]strpos, strstr and Stripos, stristr functions, Strposstristr Strpos mixed Strpos (string $haystack, mixed $needle [, int $ offset = 0]) If offset is specified, find ...

  • Related Article

    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.