3

I use CI_Minifier and have problems after I have updated my PHP.

Now I receive an error when I use the preg_match function.

if (!preg_match("/^[\w-:]+$/", $tag)) { #error line
    $node->_[HDOM_INFO_TEXT] = '<' . $tag . $this->copy_until('<>');
    if ($this->char === '<') {
        $this->link_nodes($node, false);

        return true;
    }

    if ($this->char==='>') {
        $node->_[HDOM_INFO_TEXT] .= '>';
    }
    $this->link_nodes($node, false);
    $this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next

    return true;
}

The error is:

Compilation failed: invalid range in character class at offset 4

kalehmann
  • 4,821
  • 6
  • 26
  • 36
Tomislav Tomi Nikolic
  • 608
  • 3
  • 10
  • 15

1 Answers1

12

Escape the hyphen:

if (!preg_match("/^[\w\-:]+$/", $tag)) { 

or put it at the beginning of character class:

if (!preg_match("/^[-\w:]+$/", $tag)) { 

or at the end:

if (!preg_match("/^[\w:-]+$/", $tag)) { 
Toto
  • 89,455
  • 62
  • 89
  • 125
  • I wonder how you thought to escape the hyphen? Was it a chance thing like most of my discoveries? Either way - thank you for your experience, it saved me a major brain drain and time. Is this relative to PHP versions? I wonder.. – Scott Fleming Apr 14 '21 at 12:15
  • 1
    @ScottFleming: It's a generic error. `invalid range in character class` got self explanation. To be sure, **always** escape the hyphen in a character class. – Toto Apr 14 '21 at 15:39