Created
March 15, 2010 14:20
-
-
Save evaisse/332869 to your computer and use it in GitHub Desktop.
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 #-*- coding: utf-8 -*- | |
/** | |
* Format and translit a string like "The Fantastic ~'étonnante' permalink url" => "the-fantastic-etonnante-permalink-url" | |
* | |
* @param string $string string to | |
* @param bool $dot_allowed allow dots (only one) chars in url | |
* @return string | |
*/ | |
function urlize($string, $dot_allowed=false) { | |
$dot = ($dot_allowed) ? '\.' : ''; | |
static $search, $replace; | |
if (!$search) { | |
$search = $replace = array(); | |
// Get the HTML entities table into an array | |
$trans = get_html_translation_table(HTML_ENTITIES); | |
// Go through the entity mappings one-by-one | |
foreach ($trans as $literal => $entity) { | |
// Make sure we don't process any other characters | |
// such as fractions, quotes etc: | |
if (ord($literal) >= 192) { | |
// Get the accented form of the letter | |
$search[] = utf8_encode($literal); | |
// Get e.g. 'E' from the string 'É' | |
$replace[] = $entity[1]; | |
} | |
} | |
} | |
$string = str_replace($search, $replace, $string); | |
$string = strtolower($string); | |
$find = array( | |
'/[^0-9a-z\-_'.$dot.']+/' # blacklisted chars | |
,'/[-\.]+/' # multi -, . or next to . | |
); | |
$repl = array('-',($dot_allowed) ? '.' : '-'); | |
$string = trim($string,'-.'); | |
return preg_replace($find, $repl, $string); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment