Created
December 16, 2012 13:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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