douzi7219 2013-12-03 05:59
浏览 48

preg_replace正则表达式没有按预期工作

So i'm trying to do the following and it's not working. not sure why:

$block = preg_replace('#\{([A-Z0-9\-_]+)\}#', "<?php " . $this->compile_var(\\1) . " ?>", $block);

What i'm using that for is to turn this input:

<link href="{VAR_TEMPLATE_PATH}css/common.css" rel="stylesheet" type="text/css">

into:

 <link href="<?php $this->page_vars["TEMPLATE_PATH"] ?>css/common.css" rel="stylesheet" type="text/css">

The actual conversion is fine, thats all contained in compile_var, but preg_replace is no longer turning \\1 into "VAR_TEMPLATE_PATH", which is used to do when inside a string.

Instead it is passing "\1" as the argument for compile_var?

Why is this happening all of a sudden? and how can i fix it?

Cheers!

  • 写回答

2条回答 默认 最新

  • douwei3863 2013-12-03 06:13
    关注

    Try using \1 in compile_var instead.

    <?php " . $this->compile_var(\1) . " ?>

    Here's a working example: http://regex101.com/r/rM0jD1

    评论

报告相同问题?