Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dbetts opened this issue Dec 17, 2018 · 3 comments

Comments

@dbetts
Copy link

dbetts commented Dec 17, 2018

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);
}
@jonaseberle
Copy link

With PHP 7.3 it is necessary to escape - when used in character classes but not as range.
So this is the corrected line:
$pattern = "/[^a-zA-Z\\d\\s\\-_$quotedReplacement]/u";

@odahcam
Copy link

odahcam commented Nov 3, 2022

Same error happening in PHP 8.0:

image

The error actually happen because the preg_replace in line 1174 actually replaces all characters in the stringy with '' because of the wrong regex pattern you mentioned.

This is preventing our application to go to production, is there anyway we can help you fix this or make a release with the fix get published faster?

@odahcam
Copy link

odahcam commented Nov 3, 2022

Well, I'm simply going with https://github.com/statamic/Stringy for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants