SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: substr...

  1. #1
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    substr...

    How can i limit the number of charecters from a record i've got from the database. In php i'd use the function substr.

    Thanks

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    the_string[0,the_number_of_characters]

  3. #3
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    FL
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For a "Rails" based solution there's also:
    Code:
    truncate(0, string)
    Eric A.
    Founder and CEO, XMG Networks, Inc.
    XMG Services: Web 2.0 Photo Management and Sharing | Web Hosting
    Personal Blog: From Two 2 Twelve

  5. #5
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And Rails magically knows how many characters you want

  6. #6
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You also want to look at validates_length_of (Model classmethod)

    You'd be better using validation to assert a string is too big, and let the user fix it rather than randomly truncate a string unexpectantly to the user.

    If validation is no worry, then yea, somestring[0, length] will do, or doing somestring[start..end] where start and end are integers will also work.

    validation is really the way forward though imo for this
    http://virtualfunction.net - Rails Web Development
    http://squaremove.co.uk - Rails powered Property Listings


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
  •