Skip to content

Instantly share code, notes, and snippets.

@aaronsummers
Created February 12, 2018 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronsummers/11661e32decd4ad3cefced40b071e8fe to your computer and use it in GitHub Desktop.
Save aaronsummers/11661e32decd4ad3cefced40b071e8fe to your computer and use it in GitHub Desktop.
Create a string useable as a id or class by removing spaces or special caracters
<?php
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
return strtolower($string);
}
// USE
echo clean('This is going to be cleaned!'); // Result this-is-going-to-be-cleaned
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment