关于php正则替换preg_replace的问题
替换规则如下${user.name}=><?phpecho$this->vars['user']['name']?>${user.group.id}=><?phpecho...
替换规则如下
${user.name} => <?php echo $this->vars['user']['name'] ?>
${user.group.id} => <?php echo $this->vars['user']['group']['id'] ?>
${user.group.detail.description} => <?php echo $this->vars['user']['group']['detail']['description'] ?>
关于使用preg_replace来替换 这样的一个功能 望不吝赐教啊 展开
${user.name} => <?php echo $this->vars['user']['name'] ?>
${user.group.id} => <?php echo $this->vars['user']['group']['id'] ?>
${user.group.detail.description} => <?php echo $this->vars['user']['group']['detail']['description'] ?>
关于使用preg_replace来替换 这样的一个功能 望不吝赐教啊 展开
展开全部
<?php
$str = '
${user.name}
${user.group.id}
${user.group.detail.description}
';
$search = array(
'/\${user(?:\.)([^.]*?)}/i',
'/\${user(?:\.)([^.]*?)(?:\.)([^.]*?)}/i',
'/\${user(?:\.)([^.]*?)(?:\.)([^.]*?)(?:\.)([^.]*?)}/i',
);
$replace = array(
'<?php echo $this->vars["user"]["\1"] ?>',
'<?php echo $this->vars["user"]["\1"]["\2"] ?>',
'<?php echo $this->vars["user"]["\1"]["\2"]["\3"] ?>',
);
$str = preg_replace($search, $replace, $str);
highlight_string($str);
?>
$str = '
${user.name}
${user.group.id}
${user.group.detail.description}
';
$search = array(
'/\${user(?:\.)([^.]*?)}/i',
'/\${user(?:\.)([^.]*?)(?:\.)([^.]*?)}/i',
'/\${user(?:\.)([^.]*?)(?:\.)([^.]*?)(?:\.)([^.]*?)}/i',
);
$replace = array(
'<?php echo $this->vars["user"]["\1"] ?>',
'<?php echo $this->vars["user"]["\1"]["\2"] ?>',
'<?php echo $this->vars["user"]["\1"]["\2"]["\3"] ?>',
);
$str = preg_replace($search, $replace, $str);
highlight_string($str);
?>
追问
非常感谢您的回答 但是
user name group ==都是不确定的 只不过是一种形式 \w*(\.\w*){1,} 这种形式的
您还能再帮我下嘛
追答
改成这样行了吧
vars["\1"]["\2"] ?>',
'vars["\1"]["\2"]["\3"] ?>',
'vars["\1"]["\2"]["\3"]["\4"] ?>',
);
$str = preg_replace($search, $replace, $str);
highlight_string($str);
?>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询