Regex > createSlug() method > returns slug for given value

This commit is contained in:
Meritoo
2019-02-21 23:10:53 +01:00
parent d46548d102
commit 79c09a26a6
9 changed files with 178 additions and 1 deletions

View File

@@ -12,7 +12,7 @@ use Meritoo\Common\Exception\Regex\IncorrectColorHexLengthException;
use Meritoo\Common\Exception\Regex\InvalidColorHexValueException;
/**
* Useful regular expressions methods
* Useful methods related to regular expressions
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
@@ -919,4 +919,37 @@ class Regex
return (bool)preg_match($pattern, $value);
}
/**
* Returns slug for given value
*
* @param string $value Value that should be transformed to slug
* @return string|bool
*/
public static function createSlug($value)
{
/*
* Not a scalar value?
* Nothing to do
*/
if (!is_scalar($value)) {
return false;
}
/*
* It's an empty string?
* Nothing to do
*/
if ('' === $value) {
return '';
}
$id = 'Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();';
$transliterator = \Transliterator::create($id);
$cleanValue = trim($value);
$result = $transliterator->transliterate($cleanValue);
return preg_replace('/[-\s]+/', '-', $result);
}
}