Created
May 5, 2019 20:45
-
-
Save georgestephanis/6fdb1aef16668acf5150eac20f427e5d to your computer and use it in GitHub Desktop.
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 | |
/* | |
* This is meant to be a port of the Python `slug.slug()` method, the | |
* source of which is as follows: | |
* | |
* value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore') | |
* value = re.sub('[^\w\s-]', '', value.decode('utf-8')).strip().lower() | |
* return re.sub('[-\s]+', '-', value) | |
*/ | |
function python_slug( $str ) { | |
$str = Normalizer::normalize( $str, Normalizer::FORM_KD ); | |
$str = preg_replace( '~[\x00-\x1F\x80-\xFF]~', '', $str ); | |
$str = preg_replace( '~[^\w\s-]~', '', $str ); | |
$str = strtolower( trim( $str ) ); | |
return preg_replace( '~[-\s]+~', '-', $str ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment