dracoblue.net

Convert Title-String to Url Slug in PHP or Lua

When making your urls looking better, you may want to convert a title to a url-slug.

An url-slug is in common a string containing only alphanumeric letters and '-'. Following php and lua function converts it to that format.

php:

function toSlug($str) {
return str_replace(" ","-",ereg_replace("[ ]+"," ",ereg_replace("[^ A-Za-z0-9]","",$str)));
}

lua:

function toSlug(str)
return string.gsub(string.gsub(str,"[^ A-Za-z]",""),"[ ]+","-")
end

The

toSlug function also replaces multiple spaces with one ' -' only.

You can find this Lua-How-To at luanet.net, too.

In lua, open source, php by @ 13 Jun 2007

comments powered by Disqus

Recent Files

Advertisement

Recent Dev-Articles

Read recently

About

Blogroll