Skip to content

Instantly share code, notes, and snippets.

@evaisse
Created March 15, 2010 14:20
Show Gist options
  • Save evaisse/332869 to your computer and use it in GitHub Desktop.
Save evaisse/332869 to your computer and use it in GitHub Desktop.
<?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 '&Eacute'
$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