Standardize string to a strict format

Some file systems don't accept spaces and make no difference between lowercase and uppercase characters. Also when doing mod_rewrite and you would like to standardize an URL, it's a good idea (although not always necessary) to remove white spaces and other special characters. This PHP function standardizes a string using a series of function calls and regular expressions.
1. function StandardizeString($PassedStr)
2. {
3. $PassedStr = preg_replace('/[\'"]/', '', $PassedStr); // Eliminate all quotes
4. $PassedStr = preg_replace('/[^a-zA-Z0-9]+/', '_', $PassedStr);//Remove all spaces
 5. $PassedStr = trim($PassedStr, '_'); //Remove any leading or trailing underscored
6.    $PassedStr = strtolower($PassedStr); // Lowercase everything
7.    // Return the result
8.    return $PassedStr;
9.}

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top