Last active
February 17, 2021 00:33
-
-
Save frankyonnetti/4c7bbce7a69c7fdaa915c0e5f763022a to your computer and use it in GitHub Desktop.
PHP - strip whitespace, punctuation and lowercase #php #strip-whitespace #lowercase
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
| # to keep letters & numbers | |
| $s = preg_replace('/[^a-z0-9]+/i', '_', $s); # or... | |
| $s = preg_replace('/[^a-z\d]+/i', '_', $s); | |
| # to keep letters only | |
| $s = preg_replace('/[^a-z]+/i', '_', $s); | |
| # to keep letters, numbers & underscore | |
| $s = preg_replace('/[^\w]+/', '_', $s); | |
| # strip out all whitespace | |
| $name_clean = preg_replace('/\s*/', '', $name_clean); | |
| # convert the string to all lowercase | |
| $name_clean = strtolower($name_clean); | |
| // simple | |
| print str_replace(' ', '-', strtolower($blob)); | |
| // function | |
| function clean_string($string) | |
| { | |
| $string = str_replace(' ', '-', strtolower($string)); // Replaces all spaces with hyphens. | |
| return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars. | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment