-
-
Save kunalvarma05/4308649 to your computer and use it in GitHub Desktop.
This file contains hidden or 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