Skip to content

preg_replace(): Compilation failed: invalid range in character class at offset 12 #191

@dbetts

Description

@dbetts

On line 813 of Stringify.php there is a function called slugify(). I had to add an escape character in the regex pattern on the dash or hyphen to prevent the error message. I'm not sure if that hyphen was intended to be unescaped. I made the following change to that function to prevent the error and allow the pages to load correctly.

Apparently the hyphen needs to be either the first or last character in the pattern, or escaped: invalid range in character class

public function slugify($replacement = '-')
{
$stringy = $this->toAscii();

    $quotedReplacement = preg_quote($replacement);

// $pattern = "/[^a-zA-Z\d\s-_$quotedReplacement]/u"; // Commented this out.
$pattern = "/[^a-zA-Z\d\s\-_$quotedReplacement]/u"; // Added backslash in front of the hyphen.
$stringy->str = preg_replace($pattern, '', $stringy);

    return $stringy->toLowerCase()->delimit($replacement)
                   ->removeLeft($replacement)->removeRight($replacement);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions