String.toUrlSlug
toUrlSlug($skipWords='') -> string
Description
Convert the string to a URL slug.
A “slug” is a token that is added to a URL to make it more human-readable and optimized for search engines.
'Learn HTML in 33 Days!'.toUrlSlug() //= 'learn-html-in-33-days' 'Turtles & Tide Pools'.toUrlSlug() //= 'turtles-tide-pools'
Skip Words
The $skipWords
argument takes a string containing a whitespace-delimited list of words to exclude from the slug.
This will also filter out any duplicate words from the slug.
$pageTitle = ''' THT News: The THT Festival Was a Success! ''' $pageTitle.toUrlSlug('a an the') //= 'tht-news-festival-was-success'
If $skipWords
is :common
, it will be set to a list of about 50 common English words (e.g. “and”, “or”, “a”, etc.) that will result in a cleaner, keyword-dense slug.
$pageTitle = ''' This Man Watched 'Lord of the Rings' 3,000 Times ''' $pageTitle.toUrlSlug(':common') //= 'man-watched-lord-rings-3000-times'