frankyonnetti
2/1/2020 - 8:10 PM

#php #strip-whitespace #lowercase

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.
}