Skip to content

Instantly share code, notes, and snippets.

@infn8
Created March 9, 2013 22:19
Show Gist options
  • Save infn8/5126011 to your computer and use it in GitHub Desktop.
Save infn8/5126011 to your computer and use it in GitHub Desktop.
Wordpress Style Slug Generator function in php
function generateSlug($phrase, $maxLength=50){
$result = strtolower($phrase);
$result = preg_replace("/[^a-z0-9]/", "-", $result);
$result = trim(preg_replace("/-+/", "-", $result));
$result = trim(substr($result, 0, $maxLength));
$result = preg_replace("/^-/", "", $result);
$result = preg_replace("/-$/", "", $result);
return $result;
}
// Pretty simple: keeps numbers and letters and makes everything else dashes. Removes multiple dashes in a row.
// May not always match what WP generates but will always match what it generates. Same string in = Same string out.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment