davecurrierseo
10/1/2015 - 3:55 PM

Create a web-friendly URL slug from any string

Create a web-friendly URL slug from any string

/**
 * WordPress function that sanitizes title, replaces whitespace with dashes
 * https://codex.wordpress.org/Function_Reference/sanitize_title_with_dashes
 *
 * @param  string $text
 * @return string       
 */
sanitize_title_with_dashes($string);
/**
 * Create a web-friendly URL slug from any string
 * @param  string $text
 * @return string       
 */
function create_slug($text) {
    // Lower case everything
    $text = strtolower($text);
    // Make alphanumeric (removes all other characters)
    $text = preg_replace("/[^a-z0-9_\s-]/", "", $text);
    // Clean up multiple dashes or whitespaces
    $text = preg_replace("/[\s-]+/", " ", $text);
    // Convert whitespaces and underscore to dash
    $text = preg_replace("/[\s_]/", "-", $text);

    return $text;
}