Skip to content

Instantly share code, notes, and snippets.

@jonsherrard
Created April 25, 2012 14:54
Show Gist options
  • Save jonsherrard/2490339 to your computer and use it in GitHub Desktop.
Save jonsherrard/2490339 to your computer and use it in GitHub Desktop.
Clean any string to make it URL friendly. Removes characters and whitespace
<?php
function sanitize($string, $force_lowercase = true, $anal = false) {
$strip = array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]",
"}", "\\", "|", ";", ":", "\"", "'", "&#8216;", "&#8217;", "&#8220;", "&#8221;", "&#8211;", "&#8212;",
"—", "–", ",", "<", ".", ">", "/", "?");
$clean = trim(str_replace($strip, "", strip_tags($string)));
$clean = preg_replace('/\s+/', "-", $clean);
$clean = ($anal) ? preg_replace("/[^a-zA-Z0-9]/", "", $clean) : $clean ;
return ($force_lowercase) ?
(function_exists('mb_strtolower')) ?
mb_strtolower($clean, 'UTF-8') :
strtolower($clean) :
$clean;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment