Skip to content

Instantly share code, notes, and snippets.

@rahuldadhich
Created January 10, 2018 11:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahuldadhich/34ad681ebe3a7bd57da416cceba89d31 to your computer and use it in GitHub Desktop.
Save rahuldadhich/34ad681ebe3a7bd57da416cceba89d31 to your computer and use it in GitHub Desktop.
PHP - Remove all special characters including white space, multiple space
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment