PHP中strpos、strstr和stripos、stristr函數分析_php技巧

來源:互聯網
上載者:User

本文為大家分析了 PHP中strpos、strstr和stripos、stristr函數,供大家參考,具體內容如下

strpos

mixed strpos ( string $haystack, mixed $needle [, int $offset = 0 ] )
如果offset指定了,尋找會從offset的位置開始。offset不能為負數。

返回needle第一次出現在haystack的位置。如果在haystack中找不到needle,則返回FALSE。

needle,如果needle不是字串,它會被轉換成整型數值並賦值為該數值的ASCII字元。請看下面例子。

例子

$str = "hello";$pos = strpos($str, 111);// 111的ASCII值是o,因此$pos = 4strpos核心源碼if (Z_TYPE_P(needle) == IS_STRING) {   if (!Z_STRLEN_P(needle)) {     php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty needle");     RETURN_FALSE;   }   // 調用php_memnstr函數尋找needle   found = php_memnstr(haystack + offset,              Z_STRVAL_P(needle),              Z_STRLEN_P(needle),              haystack + haystack_len);   } else {     // 如果不是字串,轉換成數字並賦值為該數位ASCII字元。     if (php_needle_char(needle, needle_char TSRMLS_CC) != SUCCESS) {        RETURN_FALSE;     }     //設定結束字元     needle_char[1] = 0;     found = php_memnstr(haystack + offset,              needle_char,              1,              haystack + haystack_len);   }}

有一點要注意的是,如果needle不是字串的話,會調用php_needle_char函數將needle轉成整型數字並轉換為其ASCII值。

尋找函數

函數最後返回的是found,php_memnstr函數實現了尋找的方法。那麼再繼續看看php_memnstr函數做了什麼:

#define php_memnstr zend_memnstr
php_memnstr是函數zend_memnstr的宏定義,查看zend_memnstr函數如下:

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;  }  // 第一個最佳化,只尋找end - needle_len次  end -= needle_len;  while (p <= end) {    // 第二個最佳化,先判斷字串的開頭和結尾是否一樣再判斷整個字串    if ((p = (char *)memchr(p, *needle, (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;}

第一個最佳化,因為(char *)memchr(p, *needle, (end-p+1)是在end – needle_len + 1(即haystack_len+1)中尋找,如果p為空白,說明needle的第一個字元在p中從未出現過。

strstr

string strstr ( string $haystack, mixed $needle [, bool $before_needle = false ] )

返回needle在haystack中第一次出現的位置到結束的字串。
這個函數的區分大小寫。

如果needle在haystack中不存在,返回FALSE。

如果before_needle為true,則返回haystack中needle在haystack第一次出現的位置之前的字串。

strstr核心源碼

if (found) {    // 計算出found的位置    found_offset = found - haystack;    if (part) {      RETURN_STRINGL(haystack, found_offset, 1);    } else {      RETURN_STRINGL(found, haystack_len - found_offset, 1);    }}

strstr函數的前半部分跟strpos類似,區別在於strstr函數在找到位置後,需要返回haystack部分的字串。part變數就是調用strstr函數時傳遞的before_needle變數。

stripos

mixed stripos ( string $haystack, string $needle [, int $offset = 0 ] )

不區分大小寫strpos。實現方式跟下面的類似,主要是使用一份拷貝然後將需要比較的字串轉換成小寫字元後進行再進行尋找。

stristr

string stristr ( string $haystack, mixed $needle [, bool $before_needle = false ] ) 不區分大小寫strstr。

核心源碼

// 拷貝一份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));  // 調用php_stristr函數找出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;}// 釋放變數efree(haystack_dup);

可以知道,found是從php_stristr中得到的,繼續查看php_stristr函數:

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);} 

這個函數的功能就是將字串都轉成小寫之後調用php_mennstr函數來尋找needle在haystack第一次出現的位置。

總結

因為strpos/stripos返回的是位置,位置從0開始計算,所以判斷尋找失敗都用=== FALSE更適合。

閱讀PHP的源碼收穫挺多,一方面可以知道某個函數的具體實現原理是怎樣的,另一方面可以學習到一些編程最佳化方案。

以上就是本文的全部內容,希望對大家學習php程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.