Skip to content

Instantly share code, notes, and snippets.

@georgestephanis
Created May 5, 2019 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georgestephanis/6fdb1aef16668acf5150eac20f427e5d to your computer and use it in GitHub Desktop.
Save georgestephanis/6fdb1aef16668acf5150eac20f427e5d to your computer and use it in GitHub Desktop.
<?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