unmatched parentheses -正则替换文件名中\过程中出错

gu1dai 2005-12-23 11:08:43
$current = 'D:\web\mambog_corp\sql\2005-12-23';
$current = preg_replace("#\\([0-9])#i", "/\\1", $current);

由于正则的转义是这样定义的。\0-37之间都会被解释为相应ascii字符。
所以我需要将文件名中的\20替换为/20

google./csdn.net/phpe.net都已经搜过了。暂且没找到答案。
...全文
594 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2005-12-23
  • 打赏
  • 举报
回复
$current = 'D:\web\mambog_corp\sql\67-12-23';
$current = preg_replace("#\\\([0-9]+)#i", "/\\1", $current);
gu1dai 2005-12-23
  • 打赏
  • 举报
回复
晕死,难道\后跟的数字统统都被解释为一个字符了?
\67得到7
\670得到不知名的字符?

$current = 'D:\web\mambog_corp\sql\67-12-23';
$current = preg_replace("#\\([0-9])#i", "/\\1", $current);

gu1dai 2005-12-23
  • 打赏
  • 举报
回复
怎么删除不了帖子?vbs脚本出错?

我搞错了,\0才会被转义。

你选择对字符 0、a、b、f、n、r、t 和 v 进行转义时需要小心,它们将被转换成 \0、\a、\b、\f、\n、\r、\t 和 \v。在 PHP 中,只有 \0(NULL)、\r(回车符)、\n(换行符)和 \t(分隔符)是预定义的转义序列, 而在 C 中,上述的所有转换后的字符都是预定义的转义序列。

然而这个问题的确暂时很困扰。
gu1dai 2005-12-23
  • 打赏
  • 举报
回复
原来问题发生在变量的传送过程中。
双引号括起的字符串中含有的\,在变量传递过程中,会被系统默认处理。\200就被处理成了那个貌似字母e被穿了一刀的样子的字符。靠。所以要用单引号。或者对所有\再加个\变成\\

$current = "D:\web\mambog_corp\sql\2005-12-23";
$current = preg_replace("#\\([0-9])#i", "/\\1", $current);
gu1dai 2005-12-23
  • 打赏
  • 举报
回复
'\' and '\\' will both end up as a single '\' in the string which
effectively reads \( to the preg parser which means you escape the '('
and so don't use it's function to capture characters for backreferences
which renders the following ')' illegal since it tries to close a not
opened '('. Therefore, \ -> \ and \\ -> \ so you need \\\ to get \\ in
the string so you don't escape the (. Clear ? :)



http://bugs.php.net/bug.php?id=16537&edit=1

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧