1. 程式人生 > >php內置函數分析之strpos()

php內置函數分析之strpos()

ces function ocr else ucc art set turn ret

 1 PHP_FUNCTION(strpos)
 2 {
 3     zval *needle;
 4     zend_string *haystack;
 5     char *found = NULL;
 6     char  needle_char[2];
 7     zend_long  offset = 0; // 搜索位置默認0
 8 
 9     ZEND_PARSE_PARAMETERS_START(2, 3)
10         Z_PARAM_STR(haystack)
11         Z_PARAM_ZVAL(needle)
12         Z_PARAM_OPTIONAL
13 Z_PARAM_LONG(offset) 14 ZEND_PARSE_PARAMETERS_END(); 15 16 /* 如果搜索起始位置offset<0, 則轉換一下 */ 17 if (offset < 0) { 18 offset += (zend_long)ZSTR_LEN(haystack); 19 } 20 /* 如果轉換之後 offset<0 或 offset大於(待查找字符串的長度),則待查找字符串中根本就沒有這個位置*/ 21 if (offset < 0 || (size_t)offset > ZSTR_LEN(haystack)) {
22 php_error_docref(NULL, E_WARNING, "Offset not contained in string"); 23 RETURN_FALSE; 24 } 25 26 /* 搜索 */ 27 if (Z_TYPE_P(needle) == IS_STRING) { // needle是字符串 28 /* needle長度為0,則報錯 */ 29 if (!Z_STRLEN_P(needle)) { 30 php_error_docref(NULL, E_WARNING, "
Empty needle"); 31 RETURN_FALSE; 32 } 33 /* 具體的搜索實現 */ 34 found = (char*)php_memnstr(ZSTR_VAL(haystack) + offset, 35 Z_STRVAL_P(needle), 36 Z_STRLEN_P(needle), 37 ZSTR_VAL(haystack) + ZSTR_LEN(haystack)); 38 } else { 39 if (php_needle_char(needle, needle_char) != SUCCESS) { 40 RETURN_FALSE; 41 } 42 needle_char[1] = 0; 43 44 found = (char*)php_memnstr(ZSTR_VAL(haystack) + offset, 45 needle_char, 46 1, 47 ZSTR_VAL(haystack) + ZSTR_LEN(haystack)); 48 } 49 50 if (found) { 51 RETURN_LONG(found - ZSTR_VAL(haystack)); 52 } else { 53 RETURN_FALSE; 54 } 55 }

php內置函數分析之strpos()