preg_replace not working when running on windows
-
For debugging purposes I have installed a wordpress instance on my local xampp install. Then I got the following errors:
PHP Warning: preg_replace(): Compilation failed: unmatched closing parenthesis at offset 19 in C:\xampp\htdocs\debug\wp-content\plugins\wp-less\lib\Stylesheet.class.php on line 88
[01-Feb-2022 13:36:39 UTC] PHP Warning: file_get_contents(C:/xampp/htdocs/red-devils/wp-content): failed to open stream: Permission denied in C:\xampp\htdocs\debug\wp-content\plugins\wp-less\vendor\wikimedia\less.php\lib\Less\Parser.php on line 682I’ve investigate what was the cause of this issues and found out, that the usage of the DIRECTORY_SEPARATOR constant in the preg_replace call was causing the trouble.
On Windows this constant has the value \ which has to be escaped in a regular expression. But on the value it runs against / is used as directory separator so escaping won’t fix the issue.
I solve the issue by replacing the constant DIRECTORY_SEPARATOR with “/” only in the preg_replace call.Before:
$lessfile_in_theme = preg_replace ('#^.*?' . DIRECTORY_SEPARATOR . $wp_content_dir . DIRECTORY_SEPARATOR . '(.*)$#', '$1', $this->stylesheet->src, 1); // the part after 'wp-content'
After:
$lessfile_in_theme = preg_replace ('#^.*?/' . $wp_content_dir . '/(.*)$#', '$1', $this->stylesheet->src, 1); // the part after 'wp-content'
- The topic ‘preg_replace not working when running on windows’ is closed to new replies.