Created
March 9, 2013 22:19
-
-
Save infn8/5126011 to your computer and use it in GitHub Desktop.
Wordpress Style Slug Generator 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 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