任何价值(在合理范围内)作为针
will be converted to char
(C中内部的数据类型,大小为一个字节)。当前strpos实现中的相关代码:
/* {{{ php_needle_char
*/
static int php_needle_char(zval *needle, char *target)
{
switch (Z_TYPE_P(needle)) {
case IS_LONG:
*target = (char)Z_LVAL_P(needle);
return SUCCESS;
case IS_NULL:
case IS_FALSE:
*target = '\0';
return SUCCESS;
case IS_TRUE:
*target = '\1';
return SUCCESS;
case IS_DOUBLE:
*target = (char)(int)Z_DVAL_P(needle);
return SUCCESS;
case IS_OBJECT:
*target = (char) zval_get_long(needle);
return SUCCESS;
default:
php_error_docref(NULL, E_WARNING, "needle is not a string or an integer");
return FAILURE;
}
}
/* }}} */
那个
(char)
var_dump(strpos('foo', ord('o') + 256));
将给出1作为答案,与
var_dump(strpos('foo', ord('o')));
.
请注意,PHP中的任何旧str*函数都是
多字节编码感知-它们只在单个字节上工作。PHP中的字符串是字节(而不是字符)的集合,并调用
strpos
只匹配一个字节值。因此,如果给它一个带多字节编码的字符串,结果就没有多大意义。
如果使用多字节编码(如utf-8),则
mbstring
strpos公司
that function is named
mb_strpos
.
mb_*
但据我记忆,这种行为已经被弃用了,而且不应该以任何方式依赖它,因为它以不明显的方式破坏代码。