Created
September 21, 2016 20:59
-
-
Save ivanbarlog/a5e80fd333819c15dce3687c52b556b3 to your computer and use it in GitHub Desktop.
Slugifier helper class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace ib\Misc; | |
/** | |
* Class Slugifier. | |
* @author Ivan Barlog <ivan.barlog@everlution.sk> | |
*/ | |
class Slugifier | |
{ | |
const DEFAULT_LOCALE = 'sk_SK.UTF8'; | |
const DEFAULT_IN_CHARSET = 'UTF-8'; | |
const DEFAULT_OUT_CHARSET = 'ASCII//TRANSLIT'; | |
/** | |
* Converts human readable text to slug. | |
* | |
* @param string $text | |
* @param string $locale | |
* | |
* @return string | |
*/ | |
public static function slugify($text, $locale = self::DEFAULT_LOCALE) | |
{ | |
setlocale(LC_ALL, $locale); | |
$normalized = iconv(self::DEFAULT_IN_CHARSET, self::DEFAULT_OUT_CHARSET, $text); | |
$cleaned = preg_replace('/[^ [:alnum:]]+/', '', strtolower($normalized)); | |
return preg_replace('/[ ]+/', '-', $cleaned); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment