谁能帮忙解释一下这个例子

Cain 2003-12-12 10:34:30
下例传回值为 $startDate = 6/19/1969

<?php
$patterns = array("/(19|20\d{2})-(\d{1,2})-(\d{1,2})/", "/^\s*{(\w+)}\s*=/");
$replace = array("\\3/\\4/\\1", "$\\1 =");
print preg_replace($patterns, $replace, "{startDate} = 1969-6-19");
?>

谁能帮忙解释一下这个例子
...全文
41 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2003-12-12
  • 打赏
  • 举报
回复
原式错
<?php
$patterns = array("/((19|20)\d{2})\-(\d{1,2})\-(\d{1,2})/", "/^\s*{(\w+)}\s*=/");
$replace = array("\\3/\\4/\\1", "$\\1 =");
print preg_replace($patterns, $replace, "{startDate} = 1969-6-19");
?>
patterns 匹配模式 replace 替换为
((19|20)\d{2})\-(\d{1,2})\-(\d{1,2}) \\3/\\4/\\1
^\s*{(\w+)}\s*= $\\1 =

preg_replace允许用数组作为参数,这样可以批量完成替换工作

xinqing6888 2003-12-12
  • 打赏
  • 举报
回复
建议学习以下正规表达式

21,893

社区成员

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

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