[PHP Source Code Reading] strpos, strstr, stripos, stristr functions, strposstristr

Source: Internet
Author: User
Tags php source code

[PHP Source Code Reading] strpos, strstr, stripos, stristr functions, strposstristr
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 = 4
Strpos 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 stringPhp_needle_charThe function converts needle to an integer and Its ASCII value.

Search functions

The function returns found,Php_memnstrThe 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 a function.Zend_memnstrTo view the zend_memnstr function:

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.PartThe variable is passed when the strstr function is called.Before_needleVariable.

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 ] )

Strstr is case-insensitive.

Core source code
// Copy a copy of haystack haystack_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 fromPhp_stristrTo continue viewing 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.

 

At the end of this article, if you have any questions or suggestions, you can have a lot of discussions, original articles, and limited writing skills. If there are any mistakes in this article, please kindly advise.

If this article is helpful to you, please refer to the following suggestions. Thank you! ^_^

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.