Skip to content

Instantly share code, notes, and snippets.

@ivanbarlog
Created September 21, 2016 20:59
Show Gist options
  • Save ivanbarlog/a5e80fd333819c15dce3687c52b556b3 to your computer and use it in GitHub Desktop.
Save ivanbarlog/a5e80fd333819c15dce3687c52b556b3 to your computer and use it in GitHub Desktop.
Slugifier helper class
<?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