0

I am trying to replace a server variable in RewriteCondition with php variable.

forexampe :

I have the following string :

$x="RewriteCond %{HTTP_HOST} ^www";

I want to replace %{VAR} with $var==

 $x="RewriteCond %{HTTP_HOST} ^www";
preg_replace("/%\{[^}]+)\}/i","$$1==",$x);

The code above is not working, I am getting a warning from php regex parser :

compilation failed, unmatched parentheses at offset at

I tried to escape $ in the replacement with a backslash, but it also didn't solve the issue.

Is there something wrong with the function?

2
  • 1
    Did you try matching the parens? Commented Dec 15, 2015 at 2:52
  • preg_replace("/\%\{[^\}]+\}/i","$$1==",$x) Commented Dec 15, 2015 at 2:56

1 Answer 1

1

The error message:

Warning: preg_replace(): Compilation failed: unmatched parentheses at offset 8

is correct. You have unmatched parentheses in your regex. The 8th character is ) (https://eval.in/485179) but there is no (.

This

$x="RewriteCond %{HTTP_HOST} ^www";
echo preg_replace("/%\{([^}]+)\}/","$$1==",$x);

would give you:

RewriteCond $HTTP_HOST== ^www

Here's a regex101 demo: https://regex101.com/r/yB0yQ5/2; and a PHP demo, https://eval.in/485180.

The i modifier also didn't make sense here since your regex had no alpha characters in it. You could add that back in if you plan to have those in the future.

Sign up to request clarification or add additional context in comments.

Comments

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.