slugify
filter वर्तमान में Django में प्रयोग किया जाता तब्दील (मोटे तौर पर) के बाद पर्ल कोड के लिए:
use Unicode::Normalize;
sub slugify($) {
my ($input) = @_;
$input = NFKD($input); # Normalize (decompose) the Unicode string
$input =~ tr/\000-\177//cd; # Strip non-ASCII characters (>127)
$input =~ s/[^\w\s-]//g; # Remove all characters that are not word characters (includes _), spaces, or hyphens
$input =~ s/^\s+|\s+$//g; # Trim whitespace from both ends
$input = lc($input);
$input =~ s/[-\s]+/-/g; # Replace all occurrences of spaces and hyphens with a single hyphen
return $input;
}
जब से तुम भी, कमज़ोर लोगों के उच्चारण वर्ण बदलने के लिए unidecode
के लिए एक कॉल में फेंक चाहते हैं (Text::Unidecode
में परिभाषित) गैर-ASCII वर्णों को अलग करने से पहले आपका सबसे अच्छा शर्त लगता है (as pointed out by phaylon)। अच्छी तरह से तार कि मुख्य रूप से ASCII हैं के लिए
use Unicode::Normalize;
use Text::Unidecode;
sub slugify_unidecode($) {
my ($input) = @_;
$input = NFC($input); # Normalize (recompose) the Unicode string
$input = unidecode($input); # Convert non-ASCII characters to closest equivalents
$input =~ s/[^\w\s-]//g; # Remove all characters that are not word characters (includes _), spaces, or hyphens
$input =~ s/^\s+|\s+$//g; # Trim whitespace from both ends
$input = lc($input);
$input =~ s/[-\s]+/-/g; # Replace all occurrences of spaces and hyphens with a single hyphen
return $input;
}
पूर्व काम करता है, लेकिन कम पड़ता है जब पूरी स्ट्रिंग गैर- ASCII वर्ण का गठन होता है, क्योंकि वे सब:
उस मामले में, समारोह की तरह लग सकता है एक खाली स्ट्रिंग के साथ छोड़कर, बाहर निकल जाओ।
नमूना उत्पादन:
string | slugify | slugify_unidecode
-------------------------------------------------
hello world hello world hello world
北亰 bei-jing
liberté liberta liberte
नोट कैसे 北 亰 Django से प्रेरित कार्यान्वयन के साथ कुछ भी नहीं करने के लिए slugifies हो जाता है। ध्यान दें कि एनएफसी सामान्यीकरण में अंतर भी है - विघटित चरित्र के दूसरे भाग को अलग करने के बाद स्वतंत्रता एनएफकेडी के साथ 'स्वतंत्रता' बन जाती है, लेकिन एनएफसी के साथ फिर से इकट्ठा 'ए' को अलग करने के बाद 'स्वतंत्रता' बन जाएगी।
ऐसा ही करें जैसा आपने जावा में किया था। क्या कोई विशेष ऑपरेशन है जिसे आप नहीं जानते कि अनुवाद कैसे करें? –
ब्रायन: हां, ऑपरेशन मुझे नहीं पता था कि अनुवाद कैसे किया जाए "स्ट्रिंग सामान्यीकृत = Normalizer.normalize (nowhitespace, Form.NFD);"। यूनिकोड :: सामान्यीकृत इसे हल करें। कैमरून का जवाब देखें। – knorv