Last active
December 24, 2015 17:49
-
-
Save c93614/6838092 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import re | |
from unicodedata import normalize | |
_punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+') | |
def slugify(text, delim=u'-'): | |
"""Generates an slightly worse ASCII-only slug.""" | |
result = [] | |
for word in _punct_re.split(text.lower()): | |
word = normalize('NFKD', word).encode('ascii', 'ignore') | |
if word: | |
result.append(word) | |
return unicode(delim.join(result)) |
This file contains hidden or 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 | |
function slugify($text, $delim='-') { | |
$results = []; | |
if (preg_match_all('/[0-9A-Za-z\x{00C0}-\x{00ff}]+/u', $text, $matchtes)) { foreach($matchtes[0] as $part) { | |
if ($p = iconv('utf8', 'ascii//TRANSLIT', $part)) { | |
array_push($results, strtolower($p)); | |
} | |
} } | |
return implode($delim, $results); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment