SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Pretty URLs

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    boston
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pretty URLs

    I'm trying to convert a name like "Charley's Steak House" to "Charleys_Steak_House".

    I have this code:
    Code:
    self.urlname = name.gsub(/\'/, '')
    self.urlname = name.gsub(/\W/, '_')
    What I was trying to do was remove any characters ' completely before I stuck in the underscores.

    Can anybody help modify that code above to:
    1. Remove any non a-z, numbers, etc completely before sticking in the underscores?
    2. Remove any double/triple/etc. instances of underscores? What sometimes happens is the it turns out as NAME____blah__asdf, i'd like to shrink that down to just one underscore?

    Thanks for any help, I'm still no good with regular expressions...

  2. #2
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. Remove any non a-z, numbers, etc completely before sticking in the underscores?

    Code:
    '1. hello ($$$) world'.gsub(/[^\w ]+/, '')
    => "1 hello  world"
    The ^ means match anything not between the square brackets (letters, numbers, space).





    2. Remove any double/triple/etc. instances of underscores? What sometimes happens is the it turns out as NAME____blah__asdf, i'd like to shrink that down to just one underscore?

    Code:
    'This    is   only        a test.'.gsub(/\W+/, '_')
    => "This_is_only_a_test_"
    The + means match one or more of the previous character.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    boston
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey forward,

    Thanks so much for the help, I really appreciate it. Worked perfectly! With what you gave me, i was also able to figure out another one after.

    Dan


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •