-
-
Save mavisland/854ec81c7837603d3cd37d5e0869110c 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
messd-up-text-just-to-stress-test-our-little-clean-url-function | |
pijamali-hasta-yagiz-sofore-cabucak-guvendi-saf-ve-haydut-kiz-cocugu-bin-plaj-gormus | |
peux-tu-maider-sil-te-plait | |
tank-efter-nu-forrn-vi-foser-dig-bort | |
aaaaaaaeceeeeiiiidnooooouuuuyssaaaaaaaeceeeeiiiidnooooouuuuyy |
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; | |
} | |
$str1 = "Mess'd up --text-- just (to) stress /test/ ?our! `little` clean url fun.ction!?-->"; | |
echo "<strong>" . slugify($str1) . "</strong>"; | |
echo "<br>"; | |
$str2 = "Pijamalı hasta yağız şoföre çabucak güvendi. Saf ve haydut kız çocuğu bin plaj görmüş."; | |
echo "<strong>" . slugify($str2) . "</strong>"; | |
echo "<br>"; | |
$str3 = "Peux-tu m'aider s'il te plaît?"; | |
echo "<strong>" . slugify($str3) . "</strong>"; | |
echo "<br>"; | |
$str4 = "Tänk efter nu – förr'n vi föser dig bort"; | |
echo "<strong>" . slugify($str4) . "</strong>"; | |
echo "<br>"; | |
$str5 = "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöùúûüýÿ"; | |
echo "<strong>" . slugify($str5) . "</strong>"; | |
echo "<br>"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment