Skip to content

Commit

Permalink
Simplify regex used in Text::slug().
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Mar 14, 2016
1 parent 5bfca19 commit 2e0ae43
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Utility/Text.php
Expand Up @@ -875,15 +875,16 @@ public static function slug($string, $options = [])
'transliteratorId' => static::$defaultTransliteratorId
];

$string = transliterator_transliterate($options['transliteratorId'], $string);

$quotedReplacement = preg_quote($options['replacement'], '/');
$map = [
'/[^\s\p{Zs}\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
'/[\s\p{Zs}]+/mu' => $options['replacement'],
'/[^\sa-zA-Z0-9]/mu' => ' ',
'/[\s]+/mu' => $options['replacement'],
sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
];

$string = preg_replace(array_keys($map), array_values($map), $string);

return transliterator_transliterate($options['transliteratorId'], $string);
return $string;
}
}
4 changes: 4 additions & 0 deletions tests/TestCase/Utility/TextTest.php
Expand Up @@ -1604,6 +1604,10 @@ public function slugInputProvider()
'Foo Bar: Not just for breakfast any-more', ['replacement' => '+'],
'Foo+Bar+Not+just+for+breakfast+any+more'
],
[
'A æ Übérmensch på høyeste nivå! И я люблю PHP! fi', [],
'A-ae-Ubermensch-pa-hoyeste-niva-I-a-lublu-PHP-fi'
],
[
'Äpfel Über Öl grün ärgert groß öko', [],
'Aepfel-Ueber-Oel-gruen-aergert-gross-oeko'
Expand Down

0 comments on commit 2e0ae43

Please sign in to comment.