Reputation: 41219
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?
Upvotes: 0
Views: 305
Reputation: 23892
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.
Upvotes: 1