Last active
January 12, 2022 13:19
-
-
Save james2doyle/9158349 to your computer and use it in GitHub Desktop.
Simple slugify function for PHP. Creates a slug for the passed string, taking into account international characters as well.
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 | |
function slugify($string, $replace = array(), $delimiter = '-') { | |
// https://github.com/phalcon/incubator/blob/master/Library/Phalcon/Utils/Slug.php | |
if (!extension_loaded('iconv')) { | |
throw new Exception('iconv module not loaded'); | |
} | |
// Save the old locale and set the new locale to UTF-8 | |
$oldLocale = setlocale(LC_ALL, '0'); | |
setlocale(LC_ALL, 'en_US.UTF-8'); | |
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $string); | |
if (!empty($replace)) { | |
$clean = str_replace((array) $replace, ' ', $clean); | |
} | |
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean); | |
$clean = strtolower($clean); | |
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean); | |
$clean = trim($clean, $delimiter); | |
// Revert back to the old locale | |
setlocale(LC_ALL, $oldLocale); | |
return $clean; | |
} |
I created ausi/slug-generator that can handle more advanced characters too, e.g.:
$generator = new Ausi\SlugGenerator\SlugGenerator;
$generator->generate('જુઓ, અત્યાર સુધી કેવી જામી નોરતાની રમઝટ', ['locale' => 'gu']);
// output: ja-o-atayara-sadhi-kavi-jami-noratani-ramajhata
The following error is being thrown when there are apostrophes (’) in the string.
iconv(): Detected an illegal character in input string
Is there a solution to this?
જુઓ, અત્યાર સુધી કેવી જામી નોરતાની રમઝટ
Finally I found someone with the exact same question
MIT license?
Yep. Do what you need to do with it
…On Sat, Jun 22, 2019, 8:34 AM Jens Törnell ***@***.***> wrote:
MIT license?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/9158349?email_source=notifications&email_token=AAK37GCF2U5BFW2N7YOWG6TP3ZBB3A5CNFSM4H2XA562YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFUD4O#gistcomment-2951111>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAK37GDUS7MRHSTGNHCIKHDP3ZBB3ANCNFSM4H2XA56Q>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
જુઓ, અત્યાર સુધી કેવી જામી નોરતાની રમઝટ