Created
July 8, 2012 16:48
-
-
Save ekka21/3071741 to your computer and use it in GitHub Desktop.
php: sanitize string
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 | |
* | |
* Parameters: | |
* $string - The string to sanitize. | |
* $force_lowercase - Force the string to lowercase? | |
* $anal - If set to *true*, will remove all non-alphanumeric characters. | |
*/ | |
function sanitize($string, $force_lowercase = true, $anal = false) { | |
$strip = array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]", | |
"}", "\\", "|", ";", ":", "\"", "'", "‘", "’", "“", "”", "–", "—", | |
"—", "–", ",", "<", ".", ">", "/", "?"); | |
$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