Skip to content

Instantly share code, notes, and snippets.

@boffey
Created January 21, 2014 13:34
Show Gist options
  • Select an option

  • Save boffey/8540107 to your computer and use it in GitHub Desktop.

Select an option

Save boffey/8540107 to your computer and use it in GitHub Desktop.

Revisions

  1. boffey created this gist Jan 21, 2014.
    15 changes: 15 additions & 0 deletions paramterize.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    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);
    }