PHP中strtr函数一些奇怪行为的解释

PHP中strtr函数一些奇怪行为的解释

前些日子,一哥们给我发了篇文章给我看,说是strtr函数有一些奇怪的行为,其地址如下:http://tech.cuit.edu.cn/forum/thread-1867-1-1.html

查看PHP的源码,得到如下解释:

【奇怪行为一】
先来看看这个php字符串替换函数 strtr()的两种状态
strtr(string,from,to)
或者strtr(string,array)
首先针对strtr函数第一种方式
我们看看下面的举例:

1
echo strtr("I Love you","Lo","lO");

得到的结果是
I lOve yOu

这个结果提醒我们
1.strtr它是区分大小写的

【源码分析一】
strtr函数的最终实现函数是string.c文件的2670行的php_strtr函数,其源码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
}

整个函数是对于256个字符进行hash替换,这256个字符中当然包括大小写字母啦

【奇怪行为二】
2.strtr的替换是很特殊的,你注意看后面那个yOu,中间的O被替换的,这显然不是我们的本意

【源码分析二】
道理同上,它是对每个字符进行对应替换,是以字符为单位,所以替换的是字符,而不是字符串

【奇怪行为三】
再举一个特殊例子,说明这个php sttr函数的怪异

1
echo strtr("I Love you","Love","");

结果是

I Love you

什么也不会改变,所以strtr需要注意的是:不能被替换为空,也就是末位那个参数不能是空字符串,当然空格是可以的。

【源码分析三】
在string.c文件的2833行,我们可以看到其调用程序如下:

1
2
3
4
5
6
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)));

MIN(Z_STRLEN_PP(from), Z_STRLEN_PP(to))是取from和to两个字符串的长度中最小的,如三所示的情况,其结果为0,从php_strtr函数中我们可以看到

1
2
3
if ((trlen < 1) || (len < 1)) {
return str;
}

当长度小于1时返回原来的字符串。所以。。。。

【奇怪行为四】
再次举例strtr函数的另一种情况

1
2
 
echo strtr("I Loves you","Love","lOvEA");

结果是

I lOvEs yOu

注意看第三个参数的A,在结果中并没有出现
【源码分析三】
其理由与二类似,MIN(Z_STRLEN_PP(from), Z_STRLEN_PP(to))是取from和to两个字符串的长度中最小的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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};
HashTable tmp_hash;
zend_hash_init(&tmp_hash, zend_hash_num_elements(hash), NULL, NULL, 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) {
zend_hash_destroy(&tmp_hash);
RETURN_FALSE;
}
zend_hash_add(&tmp_hash, string_key, string_key_len, entry, sizeof(zval*), NULL);
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);
zend_hash_add(&tmp_hash, Z_STRVAL(ctmp), len+1, entry, sizeof(zval*), NULL);
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(&tmp_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);
zend_hash_destroy(&tmp_hash);
smart_str_0(&result);
RETVAL_STRINGL(result.c, result.len, 0);
}

PHP中strtr函数一些奇怪行为的解释》上有4条评论

  1. demo

    $str=”我是好人”;
    echo strtr($str,”是”,”好”);
    得到的结果为“我好好人”。
    对于第一个源码,他不是只处理ascii吗?怎么汉字也能处理,求大侠解答。。

    回复
  2. yaoyi

    strtr函数的最终实现函数是string.h文件的2670行的php_strtr函数,其源码如下:
    ^^^
    应该是string.c
    整个目录下并没有string.h 之后php_string.h

    回复

发表评论

电子邮件地址不会被公开。 必填项已用*标注


*

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>