Created
January 21, 2014 13:34
-
-
Save boffey/8540107 to your computer and use it in GitHub Desktop.
Conversion of Rails parameterize function in php
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
function parameterize($string, $sep = '-') { | |
# Get rid of anything thats not a valid letter, number, dash and underscore and | |
# replace with a dash | |
$paramterized_string = preg_replace("/[^a-z0-9\-_]/i", $sep, $string); | |
# Remove connected dahses so we don't end up with -- anyhwere. | |
$paramterized_string = preg_replace("/$sep{2,}/", $sep, $paramterized_string); | |
# Remove any trailing spaces from the string | |
$paramterized_string = preg_replace("/^$sep|$sep$/", '', $paramterized_string); | |
# Downcase the string | |
return strtolower($paramterized_string); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! Thanks a lot for this gist.
You can also add this code at the beginning of the function to remove accents from string before parameterize it:
If you have a more elegant way to remove accents it will be appreciated :).