📄 string.c
字号:
return; } while (l > 0) { if (*t == '\\') { t++; /* skip the slash */ if (len != NULL) (*len)--; l--; if (l > 0) { if (*t == '0') { *s++='\0'; t++; } else { *s++ = *t++; /* preserve the next character */ } l--; } } else { if (s != t) { *s++ = *t++; } else { s++; t++; } l--; } } if (s != t) { *s = '\0'; }}/* }}} *//* {{{ proto string addcslashes(string str, string charlist) Escapes all chars mentioned in charlist with backslash. It creates octal representations if asked to backslash characters with 8th bit set or with ASCII<32 (except '\n', '\r', '\t' etc...) */PHP_FUNCTION(addcslashes){ zval **str, **what; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &str, &what) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(str); convert_to_string_ex(what); if (Z_STRLEN_PP(str) == 0) { RETURN_EMPTY_STRING(); } if (Z_STRLEN_PP(what) == 0) { RETURN_STRINGL(Z_STRVAL_PP(str), Z_STRLEN_PP(str), 1); } Z_STRVAL_P(return_value) = php_addcslashes(Z_STRVAL_PP(str), Z_STRLEN_PP(str), &Z_STRLEN_P(return_value), 0, Z_STRVAL_PP(what), Z_STRLEN_PP(what) TSRMLS_CC); RETURN_STRINGL(Z_STRVAL_P(return_value), Z_STRLEN_P(return_value), 0);}/* }}} *//* {{{ proto string addslashes(string str) Escapes single quote, double quotes and backslash characters in a string with backslashes */PHP_FUNCTION(addslashes){ zval **str; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(str); if (Z_STRLEN_PP(str) == 0) { RETURN_EMPTY_STRING(); } Z_TYPE_P(return_value) = IS_STRING; Z_STRVAL_P(return_value) = php_addslashes(Z_STRVAL_PP(str), Z_STRLEN_PP(str), &Z_STRLEN_P(return_value), 0 TSRMLS_CC);}/* }}} *//* {{{ proto string stripcslashes(string str) Strips backslashes from a string. Uses C-style conventions */PHP_FUNCTION(stripcslashes){ zval **str; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(str); ZVAL_STRINGL(return_value, Z_STRVAL_PP(str), Z_STRLEN_PP(str), 1); php_stripcslashes(Z_STRVAL_P(return_value), &Z_STRLEN_P(return_value));}/* }}} *//* {{{ proto string stripslashes(string str) Strips backslashes from a string */PHP_FUNCTION(stripslashes){ zval **str; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(str); ZVAL_STRINGL(return_value, Z_STRVAL_PP(str), Z_STRLEN_PP(str), 1); php_stripslashes(Z_STRVAL_P(return_value), &Z_STRLEN_P(return_value) TSRMLS_CC);}/* }}} */#ifndef HAVE_STRERROR/* {{{ php_strerror */char *php_strerror(int errnum) { extern int sys_nerr; extern char *sys_errlist[]; TSRMLS_FETCH(); if ((unsigned int)errnum < sys_nerr) return(sys_errlist[errnum]); (void)sprintf(BG(str_ebuf), "Unknown error: %d", errnum); return(BG(str_ebuf));}/* }}} */#endif/* {{{ php_stripcslashes */PHPAPI void php_stripcslashes(char *str, int *len){ char *source, *target, *end; int nlen = *len, i; char numtmp[4]; for (source=str, end=str+nlen, target=str; source < end; source++) { if (*source == '\\' && source+1 < end) { source++; switch (*source) { case 'n': *target++='\n'; nlen--; break; case 'r': *target++='\r'; nlen--; break; case 'a': *target++='\a'; nlen--; break; case 't': *target++='\t'; nlen--; break; case 'v': *target++='\v'; nlen--; break; case 'b': *target++='\b'; nlen--; break; case 'f': *target++='\f'; nlen--; break; case '\\': *target++='\\'; nlen--; break; case 'x': if (source+1 < end && isxdigit((int)(*(source+1)))) { numtmp[0] = *++source; if (source+1 < end && isxdigit((int)(*(source+1)))) { numtmp[1] = *++source; numtmp[2] = '\0'; nlen-=3; } else { numtmp[1] = '\0'; nlen-=2; } *target++=(char)strtol(numtmp, NULL, 16); break; } /* break is left intentionally */ default: i=0; while (source < end && *source >= '0' && *source <= '7' && i<3) { numtmp[i++] = *source++; } if (i) { numtmp[i]='\0'; *target++=(char)strtol(numtmp, NULL, 8); nlen-=i; source--; } else { *target++=*source; nlen--; } } } else { *target++=*source; } } if (nlen != 0) { *target='\0'; } *len = nlen;}/* }}} */ /* {{{ php_addcslashes */PHPAPI char *php_addcslashes(char *str, int length, int *new_length, int should_free, char *what, int wlength TSRMLS_DC){ char flags[256]; char *new_str = emalloc((length?length:(length=strlen(str)))*4+1); char *source, *target; char *end; char c; int newlen; if (!wlength) { wlength = strlen(what); } if (!length) { length = strlen(str); } php_charmask(what, wlength, flags TSRMLS_CC); for (source = str, end = source+length, target = new_str; (c=*source) || (source < end); source++) { if (flags[(unsigned char)c]) { if ((unsigned char)c<32 || (unsigned char)c>126) { *target++ = '\\'; switch (c) { case '\n': *target++ = 'n'; break; case '\t': *target++ = 't'; break; case '\r': *target++ = 'r'; break; case '\a': *target++ = 'a'; break; case '\v': *target++ = 'v'; break; case '\b': *target++ = 'b'; break; case '\f': *target++ = 'f'; break; default: target += sprintf(target, "%03o", (unsigned char)c); } continue; } *target++ = '\\'; } *target++ = c; } *target = 0; newlen = target-new_str; if (target-new_str < length*4) { new_str = erealloc(new_str, newlen+1); } if (new_length) { *new_length = newlen; } if (should_free) { STR_FREE(str); } return new_str;}/* }}} *//* {{{ php_addslashes */PHPAPI char *php_addslashes(char *str, int length, int *new_length, int should_free TSRMLS_DC){ return php_addslashes_ex(str, length, new_length, should_free, 0 TSRMLS_CC);}/* }}} *//* true static */const unsigned char php_esc_list[256] = {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};/* {{{ php_addslashes_ex */PHPAPI char *php_addslashes_ex(char *str, int length, int *new_length, int should_free, int ignore_sybase TSRMLS_DC){ char *e = str + (length ? length : (length = strlen(str))); char *p = str; char *new_str, *ps; int local_new_length = length; int type = (!ignore_sybase && PG(magic_quotes_sybase)) ? 1 : 0; if (!new_length) { new_length = &local_new_length; } if (!str) { *new_length = 0; return str; } /* determine the number of the characters that need to be escaped */ while (p < e) { if (php_esc_list[(int)(unsigned char)*p++] > type) { local_new_length++; } } /* string does not have any escapable characters */ if (local_new_length == length) { new_str = estrndup(str, length); goto done; } /* create escaped string */ ps = new_str = emalloc(local_new_length + 1); p = str; if (!type) { while (p < e) { int c = php_esc_list[(int)(unsigned char)*p]; if (c == 2) { *ps++ = '\\'; *ps++ = '0'; p++; continue; } else if (c) { *ps++ = '\\'; } *ps++ = *p++; } } else { while (p < e) { switch (php_esc_list[(int)(unsigned char)*p]) { case 2: *ps++ = '\\'; *ps++ = '0'; p++; break; case 3: *ps++ = '\''; *ps++ = '\''; p++; break; default: *ps++ = *p++; break; } } } *ps = '\0';done: if (should_free) { STR_FREE(str); } *new_length = local_new_length; return new_str;}/* }}} */#define _HEB_BLOCK_TYPE_ENG 1#define _HEB_BLOCK_TYPE_HEB 2#define isheb(c) (((((unsigned char) c)>=224) && (((unsigned char) c)<=250)) ? 1 : 0)#define _isblank(c) (((((unsigned char) c)==' ' || ((unsigned char) c)=='\t')) ? 1 : 0)#define _isnewline(c) (((((unsigned char) c)=='\n' || ((unsigned char) c)=='\r')) ? 1 : 0)/* {{{ php_char_to_str */PHPAPI int php_char_to_str(char *str, uint len, char from, char *to, int to_len, zval *result){ int char_count = 0; int replaced = 0; char *source, *target, *tmp, *source_end=str+len, *tmp_end = NULL; for (source = str; source < source_end; source++) { if (*source == from) { char_count++; } } if (char_count == 0) { ZVAL_STRINGL(result, str, len, 1); return 0; } Z_STRLEN_P(result) = len + (char_count * (to_len - 1)); Z_STRVAL_P(result) = target = safe_emalloc(char_count, to_len, len + 1); Z_TYPE_P(result) = IS_STRING; for (source = str; source < source_end; source++) { if (*source == from) { replaced = 1; for (tmp = to, tmp_end = tmp+to_len; tmp < tmp_end; tmp++) { *target = *tmp; target++; } } else { *target = *source; target++; } } *target = 0; return replaced;}/* }}} *//* {{{ php_str_to_str */PHPAPI char *php_str_to_str(char *haystack, int length, char *needle, int needle_len, char *str, int str_len, int *_new_length){ char *p; char *r; char *end = haystack + length; smart_str result = {0}; for (p = haystack; (r = php_memnstr(p, needle, needle_len, end)); p = r + needle_len) { smart_str_appendl(&result, p, r - p); smart_str_appendl(&result, str, str_len); } if (p < end) smart_str_appendl(&result, p, end - p); smart_str_0(&result); if (_new_length) *_new_length = result.len; return result.c;}/* }}} *//* {{{ php_str_replace_in_subject */static void php_str_replace_in_subject(zval *search, zval *replace, zval **subject, zval *result){ zval **search_entry, **replace_entry = NULL, temp_result; char *replace_value = NULL; int replace_len = 0; /* Make sure we're dealing with strings. */ convert_to_string_ex(subject); Z_TYPE_P(result) = IS_STRING; if (Z_STRLEN_PP(subject) == 0) { ZVAL_STRINGL(result, empty_string, 0, 1); return; } /* If search is an array */ if (Z_TYPE_P(search) == IS_ARRAY) { /* Duplicate subject string for repeated replacement */ *result = **subject; zval_copy_ctor(result); INIT_PZVAL(result); zend_hash_internal_pointer_reset(Z_ARRVAL_P(search)); if (Z_TYPE_P(replace) == IS_ARRAY) { zend_hash_internal_pointer_reset(Z_ARRVAL_P(replace)); } else { /* Set replacement value to the passed one */ replace_value = Z_STRVAL_P(replace); replace_len = Z_STRLEN_P(replace); } /* For each entry in the search array, get the entry */ while (zend_hash_get_current_data(Z_ARRVAL_P(search), (void **) &search_entry) == SUCCESS) { /* Make sure we're dealing with strings. */ SEPARATE_ZVAL(search_entry); convert_to_string(*search_entry); if (Z_STRLEN_PP(search_entry) == 0) { zend_hash_move_forward(Z_ARRVAL_P(search)); if (Z_TYPE_P(replace) == IS_ARRAY) { zend_hash_move_forward(Z_ARRVAL_P(replace)); } continue; } /* If replace is an array. */ if (Z_TYPE_P(replace) == IS_ARRAY) { /* Get current entry */ if (zend_hash_get_current_data(Z_ARRVAL_P(replace), (void **)&replace_entry) == SUCCESS) { /* Make sure we're dealing with strings. */ convert_to_string_ex(replace_entry); /* Set replacement value to the one we got from array */ replace_value = Z_STRVAL_PP(replace_entry); replace_len = Z_STRLEN_PP(replace_entry); zend_hash_move_forward(Z_ARRVAL_P(replace)); } else { /* We've run out of replacement strings, so use an empty one. */ replace_value = empty_string; replace_len = 0; } } if (Z_STRLEN_PP(search_entry) == 1) { php_char_to_str(Z_STRVAL_P(result), Z_STRLEN_P(result), Z_STRVAL_PP(search_entry)[0], replace_value, replace_len, &temp_result); } else if (Z_STRLEN_PP(search_entry) > 1) { Z_STRVAL(temp_result) = php_str_to_str(Z_STRVAL_P(result), Z_STRLEN_P(result), Z_STRVAL_PP(search_entry), Z_STRLEN_PP(search_entry), replace_value, replace_len, &Z_STRLEN(temp_result)); } efree(Z_STRVAL_P(result)); Z_STRVAL_P(result) = Z_STRVAL(temp_result); Z_STRLEN_P(result) = Z_STRLEN(temp_result); if (Z_STRLEN_P(result) == 0) { return; } zend_hash_move_forward(Z_ARRVAL_P(search)); } } else { if (Z_STRLEN_P(search) == 1) { php_char_to_str(Z_STRVAL_PP(subject), Z_STRLEN_PP(subject), Z_STRVAL_P(search)[0], Z_STRVAL_P(replace), Z_STRLEN_P(replace), result); } else if (Z_STRLEN_P(search) > 1) { Z_STRVAL_P(result) = php_str_to_str(Z_STRVAL_PP(subject), Z_STRLEN_PP(subject), Z_STRVAL_P(search), Z_STRLEN_P(search), Z_STRVAL_P(replace), Z_STRLEN_P(replace), &Z_STRLEN_P(result)); } else { *result = **subject; zval_copy_ctor(result); INIT_PZVAL(result); } }}/* }}} *//* {{{ proto mixed str_replace(mixed search, mixed replace, mixed subject) Replaces all occurrences of search in haystack with replace */PHP_FUNCTION(str_replace){ zval **subject, **search, **replace, **subject_entry; zval *result; char *string_key; uint string_key_len; ulong num_key; if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &search, &replace, &subject) == FAILURE) { WRONG_PARAM_COUNT; } SEPARATE_ZVAL(search); SEPARATE_ZVAL(replace); SEPARATE_ZVAL(subject); /* Make sure we're dealing with strings and do the replacement. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -