1

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!

1
  • Try \1 instead of \\1? Commented Dec 3, 2013 at 6:11

2 Answers 2

1

Use it like this:

$block = '<link href="{VAR_TEMPLATE_PATH}css/common.css" rel="stylesheet" type="text/css">';
$block=preg_replace('#\{(?:VAR_)?([\w-]+)\}#', '<?php $this->compile_var("\1") ?>', $block);
echo $block;

OUTPUT:

<link href="<?php $this->compile_var("TEMPLATE_PATH") ?>css/common.css" rel="stylesheet" type="text/css">

If you want VAR_ also in final output then use:

$block = preg_replace('#\{([\w-]+)\}#', '<?php $this->compile_var("\1") ?>', $block);
Sign up to request clarification or add additional context in comments.

Comments

0

Try using \1 in compile_var instead.

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

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

1 Comment

That just throws a syntax error: Parse error: syntax error, unexpected '\' (T_NS_SEPARATOR), expecting identifier (T_STRING) in

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.