DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Nice Post Slug

Snippets Manager user avatar by
Snippets Manager
·
Aug. 10, 06 · · Code Snippet
Like (0)
Save
Tweet
440 Views

Join the DZone community and get the full member experience.

Join For Free
This code replaces accents to normal chars(e.g "á" => "a"), everything that isn´t in "a-zA-Z0-9" to "", multiples spaces to one space, and one space to "-".

This is useful to make URLs from titles, like Netscape.com does...

"A new report recommends only work 4 -6 hrs a day" => "A-new-report-recommends-only-work-4-6-hrs-a-day"

"Video: \"Popular Mechanics\" editor debunks 9/11 myths" => "Video-Popular-Mechanics-editor-debunks-911-myths"

etc..
The code is 95% based on http://textsnippets.com/posts/show/451


def self.nice_slug(str)
		
		accents = { 
		  ['á','à','â','ä','ã'] => 'a',
		  ['Ã','Ä','Â','À','�'] => 'A',
		  ['é','è','ê','ë'] => 'e',
		  ['Ë','É','È','Ê'] => 'E',
		  ['í','ì','î','ï'] => 'i',
		  ['�','Î','Ì','�'] => 'I',
		  ['ó','ò','ô','ö','õ'] => 'o',
		  ['Õ','Ö','Ô','Ò','Ó'] => 'O',
		  ['ú','ù','û','ü'] => 'u',
		  ['Ú','Û','Ù','Ü'] => 'U',
		  ['ç'] => 'c', ['Ç'] => 'C',
		  ['ñ'] => 'n', ['Ñ'] => 'N'
		  }
		accents.each do |ac,rep|
		  ac.each do |s|
			str = str.gsub(s, rep)
		  end
		end
		str = str.gsub(/[^a-zA-Z0-9 ]/,"")
		
		str = str.gsub(/[ ]+/," ")
		

		str = str.gsub(/ /,"-")
		
		#str = str.downcase

	end
POST (HTTP)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why I'm Choosing Pulumi Over Terraform
  • Modern REST API Design Principles and Rules
  • How to Leverage Method Chaining To Add Smart Message Routing in Java
  • Debugging Deadlocks and Race Conditions

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo