Crayon Syntax Highlighter plugin fix for php 7

By | 29. January 2023
We have been quite busy for last years and our blog has been neglected during this time. Now we are back and more frequend updates are promissed. After moving to the new server, our WordPress stopped working for various reasons. One of them was Crayon Syntax Highliter plugin what gave next error:
PHP Warning:  preg_replace(): Compilation failed: 
invalid range in character class at offset 4 in /home/.../wp-content/plugins/
crayon-syntax-highlighter/crayon_langs.class.php on line 340
The cause of this error is moving to php 7 and the fact that this pugin is not anymore actively maintained. But fixing the issue is really easy. First open the plugin file crayon_langs.class.php in the text editor. Find line 340 (or whatever near it as your error message says)
return preg_replace('/[^\w-+#]/msi', '', $id);

// the hyphen need to be escaped, - -> \- 
return preg_replace('/[^\w\-+#]/msi', '', $id);
And Crayon pugin is serving you well again! As this project is defunct, another option is to switch to Urvanov Syntax Highlighter (GitHub here) which is Crayon brought up to date. Cause of this error: The “preg_replace(): Compilation failed: invalid range in character class” error message is caused by an issue with the regular expression (regex) used in the preg_replace function. It typically occurs when upgrading to a newer version of PHP, as the regex engine in newer versions may be more strict about certain syntax. In this case, the error message is indicating that there is an invalid range in a character class within the regex used in the preg_replace function. A character class is a set of characters enclosed in square brackets ([]), which specifies a set of characters that the regex should match. An invalid range would be a class that has a start and end point that are out of order or not within the ASCII range. To fix this error, you will need to review the regular expression used in the preg_replace function and make sure that all character classes are valid and that any ranges are in the correct order.

Leave a Reply