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.

49 comments on “Error: Compilation failed: invalid range in character class at offset [Solved]

    1. There can be many reasons for this issue. We can only confirm after looking into your specific case. I have sent you an email please check that and let me know if you want me to look into this.

  1. Thanks! The error was loading in between new post/product creations on a WP WooCommerce site with WP Bakery. Outside of showing the error between page loads, everything worked correctly, but this seems to have resolved the error showing. Much appreciation!

  2. OMG, I do not know how to say thank you. I was looking for a solution for more than 3 days and I could not find anything. you saved me, Bro.
    I have another problem like Sagar said so. My visual composer is not showing the backend editor structure. Could you please help me in this case?
    Thank you again.

  3. Warning: preg_match(): Compilation failed: invalid range in character class at offset 12 in \wp-content\plugins\js_composer\include\classes\shortcodes\vc-basic-grid.php on line 177

    vc-basic-grid.php: $id_pattern = ‘/’ . $this->grid_id_unique_name . ‘\:([\w-_]+)/’;

    hook-vc-grid.php . ‘([\\w-_]+)’ // 2: Shortcode name

    can you please guide me in this thanks

    this is the error i am geting i have changed the – to \- but its not working or i change the same on both file.

      1. I also have the same issue at vc-basic-grid.php: $id_pattern = ‘/’ . $this->grid_id_unique_name . ‘\:([\w-_]+)/’; ln 177. Kindly assist.

          1. Hello!
            I’m having the same issue with vc-basic-grid.php.

            $id_pattern = ‘/’ . $this->grid_id_unique_name . ‘\:([\w-_]+)/’;

            Changed to ‘\:([\w\_]+)/’ and I no longer get the error, but the grid doesn’t load.

            Please help!

            Thanks,
            Darrell.

  4. Hi,

    I have the same problem – where can I find the file to update? I am currently working with a duplicate website on my computer locally meanwhile the old version is still live.

  5. Had to leave a comment to say thank you. Only recently I noticed this error pop-up, I guess after a mandatory PHP update

  6. Dear Nabtron,
    I do not have special IT skills, but I understand this is the solution to the error (I just updated PHP version 7.3 and this WP theme use Visual Composer).
    How do I access the file you mention? ….
    To simply fix this error in visual composer for WordPress, go to file: /wp-content/plugins/js_composer/include/autoload/hook-vc-grid.php

    THANK YOU!

    1. You can access this file via ftp (filezilla) or via ssh or file manager provided by your hosting service provider. Please let me know if you want our services to fix it for you.

  7. Hey my problem is solved but the iframe is not showing, although when i am moving my cursor its showing that something is there along with url showing up at the bottom of the screen. But can’t see anything

Leave a Reply

Your email address will not be published.