scotfl.ca

Of Slugs and Permalinks and PHP

Adapted from Rick Olsen’s permalink_fu.rb.

/**
    Clean up a string and make it suitable for inclusion in a url.


    @param $str The string to be slugified

    @return a string containing a sanitized, url-safe version of $str
*/
function slugify ($str)
{
    $slug = iconv('UTF-8', 'ASCII//IGNORE//TRANSLIT', $str);
    $slug = preg_replace('/\W+/', ' ', $slug);
    $slug = trim($slug);
    $slug = strtolower($slug);
    $slug = preg_replace('/\ +/', '-', $slug);

    return $slug;
}

This entry was posted on 26 February 2007 at 07:37 and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed.