Strpos, strstr, stripos, stristr Function Analysis in PHP, strposstristr
This article analyzes strpos, strstr, stripos, and stristr functions in PHP for your reference. The specific content is as follows:
Strpos
Mixed strpos (string $ haystack, mixed $ needle [, int $ offset = 0])
If offset is specified, the query starts from the offset position. Offset cannot be negative.
Returns the position where the needle first appeared on the haystack. If no needle is found in haystack, FALSE is returned.
Needle. If needle is not a string, it is converted to an integer value and assigned an ASCII character for the value. See the following example.
Example
$ Str = "hello"; $ pos = strpos ($ str, 111); // the ASCII value of 111 is o, so $ pos = 4strpos core source code if (Z_TYPE_P (needle) = IS_STRING) {if (! Z_STRLEN_P (needle) {locate (NULL TSRMLS_CC, E_WARNING, "Empty needle"); RETURN_FALSE;} // call the php_memnstr function to find needle found = php_memnstr, z_STRVAL_P (needle), Z_STRLEN_P (needle), haystack + haystack_len);} else {// if it is not a string, convert it to a number and assign a value to the ASCII character of the number. If (php_needle_char (needle, needle_char TSRMLS_CC )! = SUCCESS) {RETURN_FALSE;} // set the end character needle_char [1] = 0; found = php_memnstr (haystack + offset, needle_char, 1, haystack + haystack_len );}}
Note that if needle is not a string, the php_needle_char function is called to convert needle into an integer and convert it to its ASCII value.
Search functions
The function returns found. The php_memnstr function implements the search method. Then let's continue to see what the php_memnstr function has done:
# Define php_memnstr zend_memnstr
Php_memnstr is the macro definition of the zend_memnstr function. view 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;} // The first optimization. Only end-needle_len end-= needle_len; while (p <= end) {// The second optimization, determine whether the start and end of the string are the same and then judge the entire string if (p = (char *) memchr (p, * needle, (en D-p + 1) & ne = p [needle_len-1]) {if (! Memcmp (needle, p, needle_len-1) {return p ;}}if (p = NULL) {return NULL ;}p ++;} return NULL ;}
First optimization, because (char *) memchr (p, * needle, (end-p + 1) is searched in end-needle_len + 1 (namely, haystack_len + 1, if p is null, the first character of needle has never appeared in p.
Strstr
String strstr (string $ haystack, mixed $ needle [, bool $ before_needle = false])
Returns the string from the position where needle first appeared in the haystack to the end.
This function is case sensitive.
If needle does not exist in haystack, FALSE is returned.
If before_needle is true, the string before the position of needle in haystack before its first appearance is returned.
Strstr core source code
If (found) {// calculate the found position 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 the strpos function. The difference is that after finding the position, the strstr function returns the haystack string. The part variable is the before_needle variable passed when the strstr function is called.
Stripos
Mixed stripos (string $ haystack, string $ needle [, int $ offset = 0])
Strpos is case-insensitive. The implementation method is similar to the following: Use a copy and convert the string to be compared to a lower-case character before searching.
Stristr
String stristr (string $ haystack, mixed $ needle [, bool $ before_needle = false]) is a case-insensitive strstr.
Core source code
// Copy a copy of haystackhaystack_dup = estrndup (haystack, haystack_len); if (Z_TYPE_P (needle) = IS_STRING) {char * orig_needle; if (! Terminate (needle) {terminate (NULL TSRMLS_CC, E_WARNING, "Empty needle"); efree (haystack_dup); RETURN_FALSE;} orig_needle = estrndup (ignore (needle ), z_STRLEN_P (needle); // call the php_stristr function to find the value of orig_needle. Found = php_stristr (haystack_dup, orig_needle, haystack_len, nested (needle); efree (orig_needle);} else {if (nested (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 {analyze (haystack + found_offset, haystack_len-found_offset, 1 );}} else {RETVAL_FALSE;} // release the variable efree (haystack_dup );
You can know that found is obtained from php_stristr. continue to view 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); return php_memnstr(s, t, t_len, s + s_len);}
This function is used to convert all strings to lowercase and call the php_mennstr function to find the position of needle in the first appearance of haystack.
Summary
Because strpos/stripos returns a position and the position starts from 0, it is more suitable to use = FALSE to determine if a search fails.
I have gained a lot from reading the source code of PHP. On the one hand, I can know the specific implementation principle of a function, and on the other hand I can learn some programming optimization solutions.
The above is all the content of this article. I hope it will help you learn php programming.