📄 string.c
字号:
*/ if (f < 0) { f = Z_STRLEN_PP(str) + f; if (f < 0) { f = 0; } } /* if "length" position is negative, set it to the length * needed to stop that many chars from the end of the string */ if (l < 0) { l = (Z_STRLEN_PP(str) - f) + l; if (l < 0) { l = 0; } } if (f >= Z_STRLEN_PP(str)) { RETURN_FALSE; } if (((unsigned) f + (unsigned) l) > Z_STRLEN_PP(str)) { l = Z_STRLEN_PP(str) - f; } RETURN_STRINGL(Z_STRVAL_PP(str) + f, l, 1);}/* }}} *//* {{{ proto string substr_replace(string str, string repl, int start [, int length]) Replaces part of a string with another string */PHP_FUNCTION(substr_replace){ zval **str; zval **from; zval **len; zval **repl; char *result; int result_len; int l; int f; int argc = ZEND_NUM_ARGS(); if (argc < 3 || argc > 4 || zend_get_parameters_ex(argc, &str, &repl, &from, &len) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(str); convert_to_string_ex(repl); convert_to_long_ex(from); if (argc > 3) { convert_to_long_ex(len); l = Z_LVAL_PP(len); } else { l = Z_STRLEN_PP(str); } f = Z_LVAL_PP(from); /* if "from" position is negative, count start position from the end * of the string */ if (f < 0) { f = Z_STRLEN_PP(str) + f; if (f < 0) { f = 0; } } else if (f > Z_STRLEN_PP(str)) { f = Z_STRLEN_PP(str); } /* if "length" position is negative, set it to the length * needed to stop that many chars from the end of the string */ if (l < 0) { l = (Z_STRLEN_PP(str) - f) + l; if (l < 0) { l = 0; } } if (((unsigned) f + (unsigned) l) > Z_STRLEN_PP(str)) { l = Z_STRLEN_PP(str) - f; } result_len = Z_STRLEN_PP(str) - l + Z_STRLEN_PP(repl); result = emalloc(result_len + 1); memcpy(result, Z_STRVAL_PP(str), f); memcpy((result + f), Z_STRVAL_PP(repl), Z_STRLEN_PP(repl)); memcpy((result + f + Z_STRLEN_PP(repl)), Z_STRVAL_PP(str) + f + l, Z_STRLEN_PP(str) - f - l); result[result_len] = '\0'; RETURN_STRINGL(result, result_len, 0);}/* }}} *//* {{{ proto string quotemeta(string str) Quotes meta characters */PHP_FUNCTION(quotemeta){ zval **arg; char *str, *old; char *old_end; char *p, *q; char c; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(arg); old = Z_STRVAL_PP(arg); old_end = Z_STRVAL_PP(arg) + Z_STRLEN_PP(arg); if (old == old_end) { RETURN_FALSE; } str = safe_emalloc(2, Z_STRLEN_PP(arg), 1); for (p = old, q = str; p != old_end; p++) { c = *p; switch (c) { case '.': case '\\': case '+': case '*': case '?': case '[': case '^': case ']': case '$': case '(': case ')': *q++ = '\\'; /* break is missing _intentionally_ */ default: *q++ = c; } } *q = 0; RETURN_STRINGL(erealloc(str, q - str + 1), q - str, 0);}/* }}} *//* {{{ proto int ord(string character) Returns ASCII value of character */PHP_FUNCTION(ord){ zval **str; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(str); RETURN_LONG((unsigned char) Z_STRVAL_PP(str)[0]);}/* }}} *//* {{{ proto string chr(int ascii) Converts ASCII code to a character */PHP_FUNCTION(chr){ zval **num; char temp[2]; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_long_ex(num); temp[0] = (char) Z_LVAL_PP(num); temp[1] = 0; RETVAL_STRINGL(temp, 1, 1);}/* }}} *//* {{{ proto string ucfirst(string str) Makes a string's first character uppercase */PHP_FUNCTION(ucfirst){ 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)) { RETURN_EMPTY_STRING(); } ZVAL_STRINGL(return_value, Z_STRVAL_PP(str), Z_STRLEN_PP(str), 1); *Z_STRVAL_P(return_value) = toupper((unsigned char) *Z_STRVAL_P(return_value));}/* }}} *//* {{{ proto string ucwords(string str) Uppercase the first character of every word in a string */PHP_FUNCTION(ucwords){ zval **str; register char *r, *r_end; 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)) { RETURN_EMPTY_STRING(); } ZVAL_STRINGL(return_value, Z_STRVAL_PP(str), Z_STRLEN_PP(str), 1); r = Z_STRVAL_P(return_value); *r = toupper((unsigned char) *r); for (r_end = r + Z_STRLEN_P(return_value) - 1; r < r_end; ) { if (isspace((int) *(unsigned char *)r++)) { *r = toupper((unsigned char) *r); } }}/* }}} *//* {{{ php_strtr */PHPAPI char *php_strtr(char *str, int len, char *str_from, char *str_to, int trlen){ int i; unsigned char xlat[256]; if ((trlen < 1) || (len < 1)) { return str; } for (i = 0; i < 256; xlat[i] = i, i++); for (i = 0; i < trlen; i++) { xlat[(unsigned char) str_from[i]] = str_to[i]; } for (i = 0; i < len; i++) { str[i] = xlat[(unsigned char) str[i]]; } return str;}/* }}} *//* {{{ php_strtr_array */static void php_strtr_array(zval *return_value, char *str, int slen, HashTable *hash){ zval **entry; char *string_key; uint string_key_len; zval **trans; zval ctmp; ulong num_key; int minlen = 128*1024; int maxlen = 0, pos, len, found; char *key; HashPosition hpos; smart_str result = {0}; zend_hash_internal_pointer_reset_ex(hash, &hpos); while (zend_hash_get_current_data_ex(hash, (void **)&entry, &hpos) == SUCCESS) { switch (zend_hash_get_current_key_ex(hash, &string_key, &string_key_len, &num_key, 0, &hpos)) { case HASH_KEY_IS_STRING: len = string_key_len-1; if (len < 1) { RETURN_FALSE; } if (len > maxlen) maxlen = len; if (len < minlen) minlen = len; break; case HASH_KEY_IS_LONG: Z_TYPE(ctmp) = IS_LONG; Z_LVAL(ctmp) = num_key; convert_to_string(&ctmp); len = Z_STRLEN(ctmp); zval_dtor(&ctmp); if (len > maxlen) maxlen = len; if (len < minlen) minlen = len; break; } zend_hash_move_forward_ex(hash, &hpos); } key = emalloc(maxlen+1); pos = 0; while (pos < slen) { if ((pos + maxlen) > slen) { maxlen = slen - pos; } found = 0; memcpy(key, str+pos, maxlen); for (len = maxlen; len >= minlen; len--) { key[len] = 0; if (zend_hash_find(hash, key, len+1, (void**)&trans) == SUCCESS) { char *tval; int tlen; zval tmp; if (Z_TYPE_PP(trans) != IS_STRING) { tmp = **trans; zval_copy_ctor(&tmp); convert_to_string(&tmp); tval = Z_STRVAL(tmp); tlen = Z_STRLEN(tmp); } else { tval = Z_STRVAL_PP(trans); tlen = Z_STRLEN_PP(trans); } smart_str_appendl(&result, tval, tlen); pos += len; found = 1; if (Z_TYPE_PP(trans) != IS_STRING) { zval_dtor(&tmp); } break; } } if (! found) { smart_str_appendc(&result, str[pos++]); } } efree(key); smart_str_0(&result); RETVAL_STRINGL(result.c, result.len, 0);}/* }}} *//* {{{ proto string strtr(string str, string from, string to) Translates characters in str using given translation tables */PHP_FUNCTION(strtr){ /* strtr(STRING, FROM, TO) */ zval **str, **from, **to; int ac = ZEND_NUM_ARGS(); if (ac < 2 || ac > 3 || zend_get_parameters_ex(ac, &str, &from, &to) == FAILURE) { WRONG_PARAM_COUNT; } if (ac == 2 && Z_TYPE_PP(from) != IS_ARRAY) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "The second argument is not an array."); RETURN_FALSE; } convert_to_string_ex(str); /* shortcut for empty string */ if (Z_STRLEN_PP(str) == 0) { RETURN_EMPTY_STRING(); } if (ac == 2) { php_strtr_array(return_value, Z_STRVAL_PP(str), Z_STRLEN_PP(str), HASH_OF(*from)); } else { convert_to_string_ex(from); convert_to_string_ex(to); ZVAL_STRINGL(return_value, Z_STRVAL_PP(str), Z_STRLEN_PP(str), 1); php_strtr(Z_STRVAL_P(return_value), Z_STRLEN_P(return_value), Z_STRVAL_PP(from), Z_STRVAL_PP(to), MIN(Z_STRLEN_PP(from), Z_STRLEN_PP(to))); }}/* }}} *//* {{{ proto string strrev(string str) Reverse a string */PHP_FUNCTION(strrev){ zval **str; char *s, *e, *n, *p; if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &str) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(str); n = emalloc(Z_STRLEN_PP(str)+1); p = n; s = Z_STRVAL_PP(str); e = s + Z_STRLEN_PP(str); while (--e>=s) { *p++ = *e; } *p = '\0'; RETVAL_STRINGL(n, Z_STRLEN_PP(str), 0);}/* }}} *//* {{{ php_similar_str */static void php_similar_str(const char *txt1, int len1, const char *txt2, int len2, int *pos1, int *pos2, int *max){ char *p, *q; char *end1 = (char *) txt1 + len1; char *end2 = (char *) txt2 + len2; int l; *max = 0; for (p = (char *) txt1; p < end1; p++) { for (q = (char *) txt2; q < end2; q++) { for (l = 0; (p + l < end1) && (q + l < end2) && (p[l] == q[l]); l++); if (l > *max) { *max = l; *pos1 = p - txt1; *pos2 = q - txt2; } } }}/* }}} *//* {{{ php_similar_char */static int php_similar_char(const char *txt1, int len1, const char *txt2, int len2){ int sum; int pos1, pos2, max; php_similar_str(txt1, len1, txt2, len2, &pos1, &pos2, &max); if ((sum = max)) { if (pos1 && pos2) sum += php_similar_char(txt1, pos1, txt2, pos2); if ((pos1 + max < len1) && (pos2 + max < len2)) sum += php_similar_char(txt1 + pos1 + max, len1 - pos1 - max, txt2 + pos2 + max, len2 - pos2 - max); } return sum;}/* }}} *//* {{{ proto int similar_text(string str1, string str2 [, float percent]) Calculates the similarity between two strings */PHP_FUNCTION(similar_text){ zval **t1, **t2, **percent; int ac = ZEND_NUM_ARGS(); int sim; if (ac < 2 || ac > 3 || zend_get_parameters_ex(ac, &t1, &t2, &percent) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(t1); convert_to_string_ex(t2); if (ac > 2) { convert_to_double_ex(percent); } if (Z_STRLEN_PP(t1) + Z_STRLEN_PP(t2) == 0) { if (ac > 2) { Z_DVAL_PP(percent) = 0; } RETURN_LONG(0); } sim = php_similar_char(Z_STRVAL_PP(t1), Z_STRLEN_PP(t1), Z_STRVAL_PP(t2), Z_STRLEN_PP(t2)); if (ac > 2) { Z_DVAL_PP(percent) = sim * 200.0 / (Z_STRLEN_PP(t1) + Z_STRLEN_PP(t2)); } RETURN_LONG(sim);}/* }}} *//* {{{ php_stripslashes * * be careful, this edits the string in-place */PHPAPI void php_stripslashes(char *str, int *len TSRMLS_DC){ char *s, *t; int l; if (len != NULL) { l = *len; } else { l = strlen(str); } s = str; t = str; if (PG(magic_quotes_sybase)) { while (l > 0) { if (*t == '\'') { if ((l > 0) && (t[1] == '\'')) { t++; if (len != NULL) (*len)--; l--; } *s++ = *t++; } else if (*t == '\\' && l > 0 && t[1] == '0') { *s++='\0'; t += 2; if (len != NULL) (*len)--; l--; } else { *s++ = *t++; } l--; } *s = '\0';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -