Skip to content

Commit

Permalink
Merge pull request #9 from Kunstmaan/fix-slugifier
Browse files Browse the repository at this point in the history
Fix slugifier
  • Loading branch information
jockri committed Jun 16, 2014
2 parents 41d37fd + 0392792 commit 579812b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
14 changes: 8 additions & 6 deletions Helper/Slugifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ class Slugifier
*
* @return string
*/
public static function slugify($text, $default = 'n-a')
public static function slugify($text, $default = 'n-a', $replace = array("'"), $delimiter = '-')
{
if (!empty($replace)) {
$text = str_replace($replace, ' ', $text);
}

// transliterate
if (function_exists('iconv')) {
$previouslocale = setlocale(LC_CTYPE, 0);
Expand All @@ -25,11 +29,9 @@ public static function slugify($text, $default = 'n-a')
setlocale(LC_CTYPE, $previouslocale);
}

$text = preg_replace('#[^\\pL\d\/]+#u', '-', $text); // replace non letter or digits by -
$text = trim($text, '-'); //trim

$text = strtolower($text); // lowercase
$text = preg_replace('#[^-\w\/]+#', '', $text); // remove unwanted characters
$text = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $text);
$text = strtolower(trim($text, $delimiter));
$text = preg_replace("/[\/_|+ -]+/", $delimiter, $text);

if (empty($text)) {
return empty($default) ? '' : $default;
Expand Down
1 change: 1 addition & 0 deletions Tests/Helper/SlugifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function getSlugifyData()
array('een titel met spaties', '', 'een-titel-met-spaties'),
array('à partir d\'aujourd\'hui', null, 'a-partir-d-aujourd-hui'),
array('CaPs ShOulD be LoweRCasEd', null, 'caps-should-be-lowercased'),
array('áàäåéèëíìïóòöúùüñßæ', null, 'aaaaeeeiiiooouuunssae'),
);
}
}

0 comments on commit 579812b

Please sign in to comment.