Hi --

On Thu, 2 Oct 2008, Ben Knight wrote:

>
> Hello.
>
> We have book titles as a column in our database, which I would like to
> use in our URLS for SEO purposes.  Given that these are titles, they
> include characters other than alphabets and numbers (e.g. punctuation,
> blanks, foreign characters in some cases).
>
> What's the easiest way to do this?  Here is some more information:
>
> Original string:
>
>      On One Flower: Butterflies, Ticks and a Few More Icks
>
>
> What I would like to see:
>
>      on-one-flower-butterflies-ticks-and-a-few-more-icks
>
>
> I'm currently doing something like this; is there a better way?
>
>      title.squeeze.downcase.tr("(),? !':.[]", "-").gsub('--', '-')

Here's one possible technique:

   title.downcase.gsub(/\W+/, '-')

\W does not include underscore (which is part of the \w class), so if
you want to translate underscores you would do:

   /[\W_]+/


David

-- 
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails  January 12-15   Fort Lauderdale, FL
   Advancing with Rails    January 19-22   Fort Lauderdale, FL *
   * Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to