📄 string.c
字号:
|| (*c == '\\' && !IsDBCSLeadByte(*(c-1)))#endif )) { c--; cnt--; } if (c+1 >= s && c < s+len-1) { buf = *(c + 1); /* Save overwritten char */ *(c + 1) = '\0'; /* overwrite char */ p = c + 1; /* Save pointer to overwritten char */ }#ifdef PHP_WIN32 if ((c = strrchr(s, '/')) || ((c = strrchr(s, '\\')) && !IsDBCSLeadByte(*(c-1)))) { if (*c == '/') { char *c2 = strrchr(s, '\\'); if (c2 && !IsDBCSLeadByte(*(c2-1)) && c2 > c) { c = c2; } }#else if ((c = strrchr(s, '/'))) {#endif ret = estrdup(c + 1); } else { ret = estrdup(s); } if (buf) *p = buf; if (buf2) *p2 = buf2; return (ret);}/* }}} *//* {{{ proto string basename(string path [, string suffix]) Returns the filename component of the path */PHP_FUNCTION(basename){ char *ret; char *string, *suffix = NULL; int string_len, suffix_len = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &string, &string_len, &suffix, &suffix_len) == FAILURE) { return; } ret = php_basename(string, string_len, suffix, suffix_len); RETURN_STRING(ret, 0);}/* }}} *//* {{{ php_dirname Returns directory name component of path */PHPAPI void php_dirname(char *path, int len){ register char *end = path + len - 1;#ifdef PHP_WIN32 /* Note that on Win32 CWD is per drive (heritage from CP/M). * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive. */ if ((2 <= len) && isalpha(path[0]) && (':' == path[1])) { /* Skip over the drive spec (if any) so as not to change */ path += 2; if (2 == len) { /* Return "c:" on Win32 for dirname("c:"). * It would be more consistent to return "c:." * but that would require making the string *longer*. */ return; } }#endif if (len <= 0) { /* Illegal use of this function */ return; } /* Strip trailing slashes */ while (end >= path && IS_SLASH_P(end)) { end--; } if (end < path) { /* The path only contained slashes */ path[0] = DEFAULT_SLASH; path[1] = '\0'; return; } /* Strip filename */ while (end >= path && !IS_SLASH_P(end)) { end--; } if (end < path) { /* No slash found, therefore return '.' */ path[0] = '.'; path[1] = '\0'; return; } /* Strip slashes which came before the file name */ while (end >= path && IS_SLASH_P(end)) { end--; } if (end < path) { path[0] = DEFAULT_SLASH; path[1] = '\0'; return; } *(end+1) = '\0';}/* }}} *//* {{{ proto string dirname(string path) Returns the directory name component of the path */PHP_FUNCTION(dirname){ zval **str; char *ret; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(str); ret = estrndup(Z_STRVAL_PP(str), Z_STRLEN_PP(str)); php_dirname(ret, Z_STRLEN_PP(str)); RETURN_STRING(ret, 0);}/* }}} *//* {{{ proto array pathinfo(string path) Returns information about a certain string */PHP_FUNCTION(pathinfo){ zval *tmp; char *path, *ret = NULL; int path_len; long opt = PHP_PATHINFO_ALL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &path, &path_len, &opt) == FAILURE) { return; } MAKE_STD_ZVAL(tmp); array_init(tmp); if ((opt & PHP_PATHINFO_DIRNAME) == PHP_PATHINFO_DIRNAME) { ret = estrndup(path, path_len); php_dirname(ret, path_len); if (*ret) add_assoc_string(tmp, "dirname", ret, 1); efree(ret); } if ((opt & PHP_PATHINFO_BASENAME) == PHP_PATHINFO_BASENAME) { ret = php_basename(path, path_len, NULL, 0); add_assoc_string(tmp, "basename", ret, 0); } if ((opt & PHP_PATHINFO_EXTENSION) == PHP_PATHINFO_EXTENSION) { char *p; int idx; int ret_len; int have_basename = ((opt & PHP_PATHINFO_BASENAME) == PHP_PATHINFO_BASENAME); /* Have we alrady looked up the basename? */ if (!have_basename) { ret = php_basename(path, path_len, NULL, 0); } ret_len = strlen(ret); p = strrchr(ret, '.'); if (p) { idx = p - ret; add_assoc_stringl(tmp, "extension", ret + idx + 1, ret_len - idx - 1, 1); } if (!have_basename) { efree(ret); } } if (opt == PHP_PATHINFO_ALL) { *return_value = *tmp; } else { zval **element; if (zend_hash_get_current_data(Z_ARRVAL_P(tmp), (void **) &element) == SUCCESS) { *return_value = **element; } else { ZVAL_EMPTY_STRING(return_value); } } zval_copy_ctor(return_value); zval_dtor(tmp); efree(tmp);}/* }}} *//* {{{ php_stristr case insensitve strstr */PHPAPI char *php_stristr(unsigned char *s, unsigned 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);}/* }}} *//* {{{ proto string stristr(string haystack, string needle) Finds first occurrence of a string within another, case insensitive */PHP_FUNCTION(stristr){ zval **haystack, **needle; char *found = NULL; int found_offset; char *haystack_orig; char needle_char[2]; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &haystack, &needle) == FAILURE) { WRONG_PARAM_COUNT; } SEPARATE_ZVAL(haystack); SEPARATE_ZVAL(needle); convert_to_string_ex(haystack); haystack_orig = estrndup(Z_STRVAL_PP(haystack), Z_STRLEN_PP(haystack)); if (Z_TYPE_PP(needle) == IS_STRING) { if (!Z_STRLEN_PP(needle)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty delimiter."); efree(haystack_orig); RETURN_FALSE; } found = php_stristr(Z_STRVAL_PP(haystack), Z_STRVAL_PP(needle), Z_STRLEN_PP(haystack), Z_STRLEN_PP(needle)); } else { convert_to_long_ex(needle); needle_char[0] = (char) Z_LVAL_PP(needle); needle_char[1] = 0; found = php_stristr(Z_STRVAL_PP(haystack), needle_char, Z_STRLEN_PP(haystack), 1); } if (found) { found_offset = found - Z_STRVAL_PP(haystack); RETVAL_STRINGL(haystack_orig + found_offset, Z_STRLEN_PP(haystack) - found_offset, 1); } else { RETVAL_FALSE; } efree(haystack_orig);}/* }}} *//* {{{ proto string strstr(string haystack, string needle) Finds first occurrence of a string within another */PHP_FUNCTION(strstr){ zval **haystack, **needle; char *found = NULL; char needle_char[2]; long found_offset; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &haystack, &needle) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(haystack); if (Z_TYPE_PP(needle) == IS_STRING) { if (!Z_STRLEN_PP(needle)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty delimiter."); RETURN_FALSE; } found = php_memnstr(Z_STRVAL_PP(haystack), Z_STRVAL_PP(needle), Z_STRLEN_PP(needle), Z_STRVAL_PP(haystack) + Z_STRLEN_PP(haystack)); } else { convert_to_long_ex(needle); needle_char[0] = (char) Z_LVAL_PP(needle); needle_char[1] = 0; found = php_memnstr(Z_STRVAL_PP(haystack), needle_char, 1, Z_STRVAL_PP(haystack) + Z_STRLEN_PP(haystack)); } if (found) { found_offset = found - Z_STRVAL_PP(haystack); RETURN_STRINGL(found, Z_STRLEN_PP(haystack) - found_offset, 1); } else { RETURN_FALSE; }}/* }}} *//* {{{ proto string strchr(string haystack, string needle) An alias for strstr *//* }}} *//* {{{ proto int strpos(string haystack, string needle [, int offset]) Finds position of first occurrence of a string within another */PHP_FUNCTION(strpos){ zval **haystack, **needle, **z_offset; char *found = NULL; char needle_char[2]; int offset = 0; int argc = ZEND_NUM_ARGS(); if (argc < 2 || argc > 3 || zend_get_parameters_ex(argc, &haystack, &needle, &z_offset) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(haystack); if (argc > 2) { convert_to_long_ex(z_offset); offset = Z_LVAL_PP(z_offset); } if (offset < 0 || offset > Z_STRLEN_PP(haystack)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset not contained in string."); RETURN_FALSE; } if (Z_TYPE_PP(needle) == IS_STRING) { if (!Z_STRLEN_PP(needle)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty delimiter."); RETURN_FALSE; } found = php_memnstr(Z_STRVAL_PP(haystack) + offset, Z_STRVAL_PP(needle), Z_STRLEN_PP(needle), Z_STRVAL_PP(haystack) + Z_STRLEN_PP(haystack)); } else { convert_to_long_ex(needle); needle_char[0] = (char) Z_LVAL_PP(needle); needle_char[1] = 0; found = php_memnstr(Z_STRVAL_PP(haystack) + offset, needle_char, 1, Z_STRVAL_PP(haystack) + Z_STRLEN_PP(haystack)); } if (found) { RETURN_LONG(found - Z_STRVAL_PP(haystack)); } else { RETURN_FALSE; }}/* }}} *//* {{{ proto int strrpos(string haystack, string needle) Finds position of last occurrence of a character in a string within another */PHP_FUNCTION(strrpos){ zval **haystack, **needle; char *found = NULL; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &haystack, &needle) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(haystack); if (Z_TYPE_PP(needle) == IS_STRING) { found = strrchr(Z_STRVAL_PP(haystack), *Z_STRVAL_PP(needle)); } else { convert_to_long_ex(needle); found = strrchr(Z_STRVAL_PP(haystack), (char) Z_LVAL_PP(needle)); } if (found) { RETURN_LONG(Z_STRLEN_PP(haystack) - strlen(found)); } else { RETURN_FALSE; }}/* }}} *//* {{{ proto string strrchr(string haystack, string needle) Finds the last occurrence of a character in a string within another */PHP_FUNCTION(strrchr){ zval **haystack, **needle; char *found = NULL; long found_offset; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &haystack, &needle) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(haystack); if (Z_TYPE_PP(needle) == IS_STRING) { found = strrchr(Z_STRVAL_PP(haystack), *Z_STRVAL_PP(needle)); } else { convert_to_long_ex(needle); found = strrchr(Z_STRVAL_PP(haystack), (char) Z_LVAL_PP(needle)); } if (found) { found_offset = found - Z_STRVAL_PP(haystack); RETURN_STRINGL(found, Z_STRLEN_PP(haystack) - found_offset, 1); } else { RETURN_FALSE; }}/* }}} *//* {{{ php_chunk_split */static PHP_ATTRIBUTE_MALLOC char *php_chunk_split(char *src, int srclen, char *end, int endlen, int chunklen, int *destlen){ char *dest; char *p, *q; int chunks; /* complete chunks! */ int restlen; chunks = srclen / chunklen; restlen = srclen - chunks * chunklen; /* srclen % chunklen */ dest = safe_emalloc(sizeof(char), (srclen + (chunks + 1) * endlen + 1), 0); for (p = src, q = dest; p < (src + srclen - chunklen + 1); ) { memcpy(q, p, chunklen); q += chunklen; memcpy(q, end, endlen); q += endlen; p += chunklen; } if (restlen) { memcpy(q, p, restlen); q += restlen; memcpy(q, end, endlen); q += endlen; } *q = '\0'; if (destlen) { *destlen = q - dest; } return(dest);}/* }}} *//* {{{ proto string chunk_split(string str [, int chunklen [, string ending]]) Returns split line */PHP_FUNCTION(chunk_split) { zval **p_str, **p_chunklen, **p_ending; char *result; char *end = "\r\n"; int endlen = 2; int chunklen = 76; int result_len; int argc = ZEND_NUM_ARGS(); if (argc < 1 || argc > 3 || zend_get_parameters_ex(argc, &p_str, &p_chunklen, &p_ending) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(p_str); if (argc > 1) { convert_to_long_ex(p_chunklen); chunklen = Z_LVAL_PP(p_chunklen); } if (argc > 2) { convert_to_string_ex(p_ending); end = Z_STRVAL_PP(p_ending); endlen = Z_STRLEN_PP(p_ending); } if (chunklen <= 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Chunk length should be greater than zero."); RETURN_FALSE; } if (chunklen > Z_STRLEN_PP(p_str)) { /* to maintain BC, we must return original string + ending */ result_len = endlen + Z_STRLEN_PP(p_str); result = emalloc(result_len + 1); memcpy(result, Z_STRVAL_PP(p_str), Z_STRLEN_PP(p_str)); memcpy(result + Z_STRLEN_PP(p_str), end, endlen); result[result_len] = '\0'; RETURN_STRINGL(result, result_len, 0); } if (!Z_STRLEN_PP(p_str)) { RETURN_EMPTY_STRING(); } result = php_chunk_split(Z_STRVAL_PP(p_str), Z_STRLEN_PP(p_str), end, endlen, chunklen, &result_len); if (result) { RETURN_STRINGL(result, result_len, 0); } else { RETURN_FALSE; }}/* }}} *//* {{{ proto string substr(string str, int start [, int length]) Returns part of a string */PHP_FUNCTION(substr){ zval **str, **from, **len; int l; int f; int argc = ZEND_NUM_ARGS(); if (argc < 2 || argc > 3 || zend_get_parameters_ex(argc, &str, &from, &len) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(str); convert_to_long_ex(from); if (argc > 2) { 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -