PHP Strpos, Strstr and Stripos, stristr function Analysis _php techniques

Source: Internet
Author: User
Tags learn php learn php programming lowercase mixed numeric value php programming

This article for everyone to analyze the PHP strpos, strstr and Stripos, Stristr function, for your reference, the specific contents are as follows

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, Mr);
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 needle
   found = php_memnstr (haystack + offset,
              z_strval_p (needle),
              z_strlen_p ( Needle),
              haystack + haystack_len);
   } else {
     //if not a string, converts to a number and assigns a value 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.

Lookup function

The function finally returns the FOUND,PHP_MEMNSTR function to implement the lookup method. So go ahead and see what the PHP_MEMNSTR function does:

#define PHP_MEMNSTR Zend_memnstr
PHP_MEMNSTR is the macro definition of the 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 = Haystac K;
  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 find the End-needle_len second end-
  = Needle_len;

  while (P-<= end) {
    //second optimization, determine whether the beginning and end of the string are the same, and then judge 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 null, the first character of Needle is never seen in P.

Strstr

String Strstr (String $haystack, mixed $needle [, bool $before _needle = false])

Returns the first occurrence of needle in haystack to the end of the string.
The case of this function is case-sensitive.

Returns False if needle does not exist in haystack.

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

STRSTR Core Source

if (found) {
    ///calculates the position of the 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 the Strpos, except that the STRSTR function needs to return the haystack part of the string when it finds a location. The part variable is the Before_needle variable that is passed when the STRSTR function is invoked.

Stripos

Mixed Stripos (String $haystack, string $needle [, int $offset = 0])

Case-insensitive Strpos. The implementation is similar to the following, primarily by using a copy and then by converting the strings that need to be compared to lowercase characters and then searching.

Stristr

string Stristr (String $haystack, mixed $needle [, bool $before _needle = false]) are case-insensitive strstr.

Core source

/copy of haystack Haystack_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 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);

The function is to call the Php_mennstr function after the string has been converted to lowercase to find the first occurrence of needle in haystack.

Summarize

Because Strpos/stripos returns the position, the position is calculated from 0, so it is more appropriate to judge the lookup failure with = = False.

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

The above is the entire content of this article, I hope that you learn PHP programming help.

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.