In-depth analysis of strpos function in PHP

Source: Internet
Author: User
Tags define function
This article to share the content is about PHP strpos function in-depth analysis, the content is very detailed, the need for friends can refer to, hope can help you.

Overview

In PHP strpos , it is often used to determine whether a string exists in another string, and this article describes the strpos function and its implementation.

Strpos applications

<?php/* Strpos example *///testecho ' match: ', Strpos (' xasfsdfbk ', ' XASFSDFBK ')!== false? ' True ': ' false ', '; ', Php_eol;echo ' match: ', Strpos (' xasfsdfbk ', ' FBK ')!== false? ' True ': ' false ', '; ', Php_eol;echo ' match: ', Strpos (' xasfsdfbk ', ' xs ')! = False? ' True ': ' false ', '; ', Php_eol;echo ' match: ', Strpos (' xasfsdfbk ', ' SFS ')!== false? ' True ': ' false ', '; ', php_eol;//codestrpos (' xasfsdfbk ', ' SFS ');
Warning: The strpos function may return a Boolean value FALSE , but it may also return FALSE an equivalent non-boolean value. Please read the Boolean type section for more information. You should use === an operator to test the return value of this function.

Strpos Series functions

functions description version
Strpos where to find the first occurrence of a string PHP 4, PHP 5, PHP 7
stripos Find where strings first appear (case-insensitive) PHP 5, PHP 7
Strrpos calculates the location of the last occurrence of the specified string in the target string php 4, PHP 5, PHP 7
Strripos calculates the last occurrence of the specified string in the target string (case-insensitive) PHP 5, PHP 7
mb_strpos Find where the string first appears in another string php 4 >= 4.0.6, PHP 5, PHP 7
strstr Find the first occurrence of a string php 4, PHP 5, PHP 7
stristr strstr () function ignore case version PHP 4, PHP 5, PHP 7
Substr_count counts the number of occurrences of a string php 4, PHP 5, PHP 7
mb* related functions are also available, such as Mb_strpos, which performs a multi-byte secure Strpos () operation based on the number of characters.

PHP (strpos) source code

Strpos (EXT/STANDARD/STRING.C)

    • PHP Source Address

Php_function (strpos) {zval *needle;    Zend_string *haystack;    char *found = NULL;    Char needle_char[2]; Zend_long offset = 0; #ifndef fast_zpp if (Zend_parse_parameters (Zend_num_args (), "Sz|l", &haystack, &needle, &    Amp;offset) = = FAILURE) {return; } #else Zend_parse_parameters_start (2, 3) z_param_str (haystack) z_param_zval (needle) z_param_option AL Z_param_long (offset) zend_parse_parameters_end (); #endif if (offset < 0) {offset + = (Zend_long) Z    Str_len (haystack); } if (Offset < 0 | | |        (size_t) offset > Zstr_len (haystack)) {php_error_docref (NULL, e_warning, "offset not contained in string");    Return_false; } if (z_type_p (needle) = = is_string) {if (!            Z_strlen_p (needle)) {Php_error_docref (NULL, e_warning, "Empty needle");        Return_false;   } found = (char*) php_memnstr (Zstr_val (haystack) + offset, z_strval_p (needle),                         Z_strlen_p (needle), Zstr_val (haystack) + Zstr_len (haystack));        } else {if (Php_needle_char (needle, needle_char)! = SUCCESS) {return_false;        } needle_char[1] = 0;                            Found = (char*) php_memnstr (Zstr_val (haystack) + offset, Needle_char,    1, Zstr_val (haystack) + Zstr_len (haystack));    } if (found) {Return_long (Found-zstr_val (haystack));    } else {return_false; }}

Php_memnstr (Main/php.h)

    • PHP Source Address

#define PHP_MEMNSTR ZEND_MEMNSTR/* 338 line*/

Zend_memnstr (ZEND/ZEND_OPERATORS.H)

    • PHP Source Address

/* * This function is to find needle in haystack, if there is no return null, if present, returns a pointer to needle header character in haystack */zend_memnstr (const char *haystack, const    Char *needle, size_t needle_len, const char *end) {const char *p = haystack;    const char NE = needle[needle_len-1];    ptrdiff_t off_p;    size_t off_s;    if (Needle_len = = 1) {return (const char *) MEMCHR (P, *needle, (end-p));    } off_p = End-haystack; off_s = (off_p > 0)?    (size_t) off_p:0;    if (Needle_len > off_s) {return NULL;        } if (Expected (off_s < 1024x768 | | Needle_len < 3)) {//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 = (const char *) MEMCHR (p, *nee                    Dle, (end-p+1))) && ne = = p[needle_len-1]) {if (!memcmp (needle, p, needle_len-1)) {                return p;            }} if (p = = null) {return null;        } p++;    }    return NULL;    } else {return zend_memnstr_ex (haystack, needle, needle_len, end); }}

MEMCHR (string.h)

    • Linux kernel version-source address

/* header file: #include <string.h> define function: void * MEMCHR (const void *s, char C, size_t n); function Description: MEMCHR () searches from the beginning to the first n bytes of the memory content referred to by S, until a Now the first byte with a value of C, a pointer to that byte is returned. Return value: Returns a pointer to the byte if the specified byte is found, otherwise 0 is returned. */#ifndef __have_arch_memchrvoid *MEMCHR (const void *s, int c, size_t N) {    const unsigned char *p = s;    while (n--! = 0) {            if ((unsigned char) c = = *p++) {            return (void *) (p-1);        }    }    return NULL;} Export_symbol (MEMCHR); #endif

MEMCMP (string.h)

    • Linux kernel version-source address

/* String function memcmp   prototype: extern int memcmp (void *buf1, void *buf2, unsigned int count);    Function: Compare the memory area BUF1 and Buf2 before count bytes Description: When Buf1<buf2, return value <0 when buf1=buf2, return value     =0         when buf1>buf2, return value >0                                        */#ifndef __have_arch_memcmp#undef memcmp__visible int MEMCMP (const void *cs, const void *CT, size_t count) {
  const unsigned char *su1, *SU2;    int res = 0;    for (SU1 = cs, su2 = ct; 0 < count; ++su1, ++SU2, count--)        if (res = *SU1-*su2)! = 0) break            ;    return res;} Export_symbol (memcmp); #endif 

Tips

The Strpos function is case-sensitive.

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.