r_replace_in_subject
复制代码 代码如下:
/* {{{ php_str_replace_in_subject
*/
static void php_str_replace_in_subject(zval *search, zval *replace, zval **subject, zval *result, int case_sensitivity, int *replace_count)
{
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, "", 0, 1);
return;
}
/* If search is an array */
if (Z_TYPE_P(search) == IS_ARRAY) {
...//不走这步
} else {
if (Z_STRLEN_P(search) == 1) { //例子中只有”#“所以,执行这一步。
php_char_to_str_ex(Z_STRVAL_PP(subject),//subject的值,也就是dizaz#7#final
Z_STRLEN_PP(subject), //获取subject的长度
Z_STRVAL_P(search)[0], //由于只有1个”#”,所以只需要第一个字符
Z_STRVAL_P(replace), //所要替换成的字符,现在是“-”
Z_STRLEN_P(replace), //目标字符的长度,现在为1
result, //替换结果
case_sensitivity, //大小写是否敏感,默认是1
replace_count); //替换次数
} else if (Z_STRLEN_P(search) > 1) {
Z_STRVAL_P(result) = php_str_to_str_ex(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), case_sensitivity, replace_count);
} else {
MAKE_COPY_ZVAL(subject, result);
}
}
}
到现在为止,我们的目标最终锁定到了php_char_to_str_ex 函数,现在只需要分析这个函数就OK了。其实现为:
复制代码 代码如下:
/* {{{ php_char_to_str_ex
*/
PHPAPI int php_char_to_str_ex(char *str, uint len, char from, char *to, int to_len, zval *result, int case_sensitivity, int *replace_count)
{
int char_count = 0;
int replaced = 0;
char *source, *target, *tmp, *source_end=str+len, *tmp_end = NULL;
if (case_sensitivity) { //现在case_sensitivity = 1
char *p = str, *e = p + len;
//计算需要替换几次
while ((p = memchr(p, from, (e - p)))) {
char_count++;
p++;
}
} else {
for (source = str; source < source_end; source++) {
if (tolower(*source) == tolower(from)) {
char_count++;
}
}
}
if (char_count == 0 && case_sensitivity) {
ZVAL_STRINGL(result, str, len, 1);
return 0;
}
//计算替换以后的长度,并且存储到result中。
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;
//target跟result的值都指向统一块内存,所以只需要处理target
if (case_sensitivity) {
char *p = str, *e = p + len, *s = str;
while ((p = memchr(p, from, (e - p)))) { //判断在第几个字符出现#
memcpy(target, s, (p - s)); //把#以前的数据拷贝给target
target += p - s;
memcpy(target, to, to_len); //把目标字符拷贝给target[当然此时的target是开始target+p-s的]
target += to_len;
p++;
s = p;
if (replace_count) {
*replace_count += 1; //设定替换次数
}
}
//如果后面还有,继续添加到target后,这样target所指向的内存块已经是替换好的数据了。
if (s < e) {
memcpy(target, s, (e - s));
target += e - s;
}
} else {
for (source = str; source <