Created
February 12, 2018 09:55
-
-
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
This file contains 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
<?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