I have a more detailed comment on the PHP source code on GitHub. Interested can be onlookers, to a star. PHP5.4 source annotation. Comments that have been added can be viewed through a commit record.
Strpos
Mixed Strpos (String $haystack, mixed $needle [, int $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);//111 ASCII value 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 needle found = 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 character needle_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 method that the FOUND,PHP_MEMNSTR function implements to find. 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:
Static inline char *zend_memnstr (char *haystack, char *needle, int needle_len, char *end) { char *p = haystack; Char ne = needle[needle_len-1]; if (Needle_len = = 1) { return (char *) MEMCHR (P, *needle, (end-p)); } if (Needle_len > End-haystack) { return NULL; } First optimization, find only end-needle_len times end-= Needle_len; while (P <= end) { ///second optimization, first determine whether the beginning and end of the string are the same again to determine the entire string if (p = (char *) MEMCHR (P, *needle, (end-p+1))) &&am P NE = = p[needle_len-1]) { if (!memcmp (needle, p, needle_len-1)) { return p; } } if (p = = null) { return null; } p++; } return NULL;}
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 [, bool $before _needle = false]) returns the position of the first occurrence of the 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) { Return_stringl (haystack, found_offset, 1); } else { Return_stringl (found, haystack_len-found_ Offset, 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 [, int $offset = 0]) are 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 [, bool $before _needle = false]) is case-insensitive strstr.
Core source
Copies a copy of 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;} Release variable Efree (haystack_dup);
As you can see, found is obtained from PHP_STRISTR and continues to look at the PHP_STRISTR function:
Phpapi Char *php_stristr (char *s, char *t, size_t S_len, size_t t_len) { Php_strtolower (s, s_len); Php_strtolower (t, T_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.
Finally Amway again, I have a more detailed comment on the PHP source code in GitHub. Interested can be onlookers, to a star. PHP5.4 source annotation. Comments that have been added can be viewed through a commit record.
Appreciation support Author write more Good articles, thank you!
Reward authors
Appreciation support Author write more Good articles, thank you!
Choose a payment method
About the Author: hoohack
A rookie who's trying to make a personal homepage · My article · 3 ·