Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save frankyonnetti/4c7bbce7a69c7fdaa915c0e5f763022a to your computer and use it in GitHub Desktop.

Select an option

Save frankyonnetti/4c7bbce7a69c7fdaa915c0e5f763022a to your computer and use it in GitHub Desktop.
PHP - strip whitespace, punctuation and lowercase #php #strip-whitespace #lowercase
# 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