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;
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

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

Back To Top