Skip to content

Instantly share code, notes, and snippets.

@c93614
Last active December 24, 2015 17:49
Show Gist options
  • Save c93614/6838092 to your computer and use it in GitHub Desktop.
Save c93614/6838092 to your computer and use it in GitHub Desktop.
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))
<?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