Site icon Nabtron

Error: Compilation failed: invalid range in character class at offset [Solved]

If you recently updated your server to PHP 7.3, you might have seen this error message:

Compilation failed: invalid range in character class at offset . . .

This error is due to update in PHP 7.3 and how it handles regular expressions.

Error: Compilation failed: invalid range in character class at offset

I encountered this error on a WordPress site running WP Bakery Page Builder / visual composer 5.6 plugin. The error said:

Compilation failed: invalid range in character class at offset 11 in /home/path/public_html/content/plugins/js_composer/include/autoload/hook-vs-grid.php on line 163

The plugin came with the theme we were using and the theme was at latest version but had visual composer version 5.6 only, thus we didn’t have any other option than to fix this error, otherwise we would have to purchase the wp bakery visual composer latest update (5.7).

This post will help you fix this issue in visual composer or any other PHP plugin or code if you’re experiencing it.

How to fix Error Compilation failed: invalid range in character class at offset

To simply fix this error in visual composer for WordPress, go to file:

/wp-content/plugins/js_composer/include/autoload/hook-vc-grid.php

And edit line 86, from:

        . '([\\w-_]+)'                     // 2: Shortcode name

To:

        . '([\\w\-_]+)'                     // 2: Shortcode name

Save the file and you’re done!

We changed the – to \- (if you’re wondering).

Explanation

The here is intended to match the literal character. However it’s also used for giving a range, like 1-7 or so. PHP 7.3 expects us to be more clear in this situation if we want to use the literal character. So what we did here is that we escaped the character by adding a backslash to it like this: \- and it fixed our code.

Note that we don’t need to make that change everywhere in our code. We need to do that where PHP 7.3 more likely takes this literal character as a range in that statement. However it’s a good practice to escape it more often.

You can use this logic to fix this error in any other of your scripts too. If you’re confused and want my help to fix it, please let me know.

Exit mobile version