警告:preg_replace() [function.preg-replace]:编译失败:在偏移 1 处无需重复 [英] Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 1
问题描述
有人可以帮忙调试这个错误吗?
Can someone help debug this error?
警告:preg_replace() [function.preg-replace]:编译失败:在偏移量 1 处无需重复
Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 1
//Generate uid
function gen_uid($len=40) {
$hex = md5("what" . uniqid("", true));
$pack = pack('H*', $hex);
$tmp = base64_encode($pack);
$uid = preg_replace("#(*UTF8)[^A-Za-z0-9]#", "", $tmp);
$len = max(4, min(128, $len));
while (strlen($uid) < $len)
$uid .= gen_uid(22);
return substr($uid, 0, $len);
}
这是什么原因造成的?这是PHP问题还是其他问题?该应用程序在我的本地机器上运行良好,但在服务器上运行不正常.
What causes this? Is it a PHP issue or something else? The application works fine on my local machine but not on the server.
推荐答案
*
in regex 表示匹配前一个字符 0 次或多次,而 (
开始捕获组.所以,*
没有什么可重复的,因为在 *
之前的是一个 (
,它不能单独重复,因此这个警告.
*
in regex means to match the previous character 0 or more times, while (
starts a capturing group. So, the *
has nothing to repeat, since what comes before the *
is a (
, which cannot be repeated by itself, hence this warning.
要修复它,只需转义 *
,就像这样:
To fix it, just escape the *
, like so:
$uid = preg_replace("#(\*UTF8)[^A-Za-z0-9]#", "", $tmp);
这篇关于警告:preg_replace() [function.preg-replace]:编译失败:在偏移 1 处无需重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!