Created
November 20, 2014 04:37
-
-
Save RoverWire/246251d5ce80b845032f to your computer and use it in GitHub Desktop.
String to Slug Converter
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
/** | |
* URL Slug | |
* | |
* Converts an string into a formated url slug string | |
* | |
* @access public | |
* @param string str | |
* @return string | |
*/ | |
if (! function_exists('url_slug')) | |
{ | |
function url_slug($str) | |
{ | |
$a = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿŔŕ'; | |
$b = 'aaaaaaaceeeeiiiidnoooooouuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRr'; | |
$str = utf8_decode($str); | |
$str = strtr($str, utf8_decode($a), $b); | |
$str = strtolower(trim($str)); | |
$str = preg_replace("/[^a-z0-9-]/", "-", $str); | |
$str = preg_replace("/-+/", "-", $str); | |
if(substr($str, strlen($str) - 1, strlen($str)) === "-") { | |
$str = substr($str, 0, strlen($str) - 1); | |
} | |
return utf8_encode($str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment