Skip to content

Instantly share code, notes, and snippets.

@ichiriac
Created December 16, 2012 13:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ichiriac/4307267 to your computer and use it in GitHub Desktop.
Save ichiriac/4307267 to your computer and use it in GitHub Desktop.
This function sanitize strings by removing accentuation, converting UTF8 to ASCII - helpfull for encoding URLs
/**
* Function: sanitize
* Returns a sanitized string, typically for URLs.
*
* Parameters:
* $string - The string to sanitize.
* $lowercase - Force the string to lowercase?
* $alnum - If set to *true*, will remove all non-alphanumeric characters.
*/
function sanitize($string, $lowercase = true, $alnum = false) {
$string = trim(
strtr(
strip_tags(
str_replace(
array('`', '^', '\''), null,
iconv(
'UTF-8',
'US-ASCII//TRANSLIT//IGNORE',
strtr($string, '\'', ' ')
)
)
),
'~`!@?#$%§^&*()_=+[]{}\\/|;:,"\'<>.',
' '
)
);
if ($alnum) $string = preg_replace('/[^a-zA-Z0-9]/', ' ', $string);
if ($lowercase) $string = strtolower($string);
return preg_replace('/\s+/', '-', $string);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment