String manipulation

I need to make some nicenames from my post titles, like:

  • convert spaces to “-”
  • delete all the !,?,commas,.,;.:,* from the string

How can I do? Sorry for the noob question…

Oh, and another thing… How can I make permalink this way:
Not …/blog/show/4 but …/category/nicename ?

Thankyou.

On 3/24/06, Dan [email protected] wrote:

I need to make some nicenames from my post titles, like:

  • convert spaces to “-”
  • delete all the !,?,commas,.,;.:,* from the string

How can I do? Sorry for the noob question…

Lookup the gsub! method in the String documentation. (Look in the
Ruby docs, not the Rails docs.)

Oh, and another thing… How can I make permalink this way:
Not …/blog/show/4 but …/category/nicename ?

http://manuals.rubyonrails.com/read/chapter/65

On Fri, Mar 24, 2006 at 06:33:37PM +0100, Dan wrote:

I need to make some nicenames from my post titles, like:

  • convert spaces to “-”
  • delete all the !,?,commas,.,;.:,* from the string

How can I do? Sorry for the noob question…

Oh, and another thing… How can I make permalink this way:
Not …/blog/show/4 but …/category/nicename ?

you should have a look at the typo code (typo.leetsoft.com). Imho it
solves both issues you mentioned.

Regards,
Jens

Hi Dan,

I need to make some nicenames from my post titles, like:

  • convert spaces to “-”
  • delete all the !,?,commas,.,;.:,* from the string

Try something like:

delete all the characters: (Add, or modify characters as needed)

link=@article[:title].gsub(/(['*&^%$+_))((**&&^%$#@!~`{}/.,;])/,
‘’ )

create permalink:

[root@srv31 ror]# ruby script/console
Loading development environment.
str=>> str=“My long string”
=> “My long string”

def create_permalink(string)
string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
?> ‘%’ + $1.unpack(‘H2’ * $1.size).join(‘%’).upcase
end.tr(’ ', ‘-’).downcase

?> end
=> nil

permalink=create_permalink(str)
=> “my-long-string”

Modify to your preferences.

Oh, and another thing… How can I make permalink this way:
Not …/blog/show/4 but …/category/nicename ?

Try something like this in routes.rb

map.connect ‘category/:link’, :controller => “blog”, :action => ‘show’

Hope this helps.

Frank

Dan [email protected] wrote:

Want to read my Pro Rails book or Pro Server Management book? Become a
beta reader now. Write me to learn more.

Rails Blog: http://railsruby.blogspot.com
MySQL Blog: http://mysqldatabaseadministration.blogspot.com
Linux / Security Blog: http://frankmash.blogspot.com
Programming One Liners: http://programming-oneliners.blogspot.com