Skip to content

Instantly share code, notes, and snippets.

@gbutiri
Created December 7, 2017 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbutiri/f3c71b8938918fc965a00211a5d41565 to your computer and use it in GitHub Desktop.
Save gbutiri/f3c71b8938918fc965a00211a5d41565 to your computer and use it in GitHub Desktop.
CodeIgniter Helper - Friendly URL
<?php
function friendlyURL($string, $is_file = false, $delimiter = '-'){
if ($is_file) {
$name = pathinfo($string, PATHINFO_FILENAME);
$ext = pathinfo($string, PATHINFO_EXTENSION);
$filename = friendlyUrl($name) . '.' . friendlyUrl($ext);
return $filename;
} else {
$string = preg_replace("`\[.*\]`U","",$string);
$string = preg_replace('`&(amp;)?#?[a-z0-9]+;`i', $delimiter, $string);
$string = htmlentities($string, ENT_COMPAT, 'utf-8');
$string = preg_replace( "`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i","\\1", $string );
$string = preg_replace( array("`[^a-z0-9]`i","`[-]+`") , $delimiter, $string);
}
return strtolower(trim($string));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment