Skip to content

Instantly share code, notes, and snippets.

@stoll
Created July 3, 2018 15:16
Show Gist options
  • Select an option

  • Save stoll/c79bb96b068bed37f39220b30ac72a38 to your computer and use it in GitHub Desktop.

Select an option

Save stoll/c79bb96b068bed37f39220b30ac72a38 to your computer and use it in GitHub Desktop.

Revisions

  1. stoll created this gist Jul 3, 2018.
    18 changes: 18 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@

    function str_slug($title, $separator = '-') {
    // Convert all dashes/underscores into separator
    $flip = $separator == '-' ? '_' : '-';

    $title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);

    // Replace @ with the word 'at'
    $title = str_replace('@', $separator.'at'.$separator, $title);

    // Remove all characters that are not the separator, letters, numbers, or whitespace.
    $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', mb_strtolower($title));

    // Replace all separator characters and whitespace by a single separator
    $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);

    return trim($title, $separator);
    }