Skip to content

Instantly share code, notes, and snippets.

@kunalvarma05
Forked from ichiriac/sanitize.php
Created December 16, 2012 15:41
Show Gist options
  • Save kunalvarma05/4308649 to your computer and use it in GitHub Desktop.
Save kunalvarma05/4308649 to your computer and use it in GitHub Desktop.
/**
* 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