'한글처리'에 해당되는 글 1건

  1. [2006/11/29] Rails에서 한글 truncate method

Rails에서 한글 truncate method

[Development]
Rails의 ActionView::Helpers::TextHelper에 있는 truncate method는 한글을 자를 때, 정확히 처리하지를
못한다. 다음의 truncate method를 application_helper에 등록해 두면, view page에서 한글을 정확히 자를 수
있다. UTF-8 문자를 한글, 영어 모두 1글자씩으로 처리해서 자르게된다.

  1. def truncate(text, len, tail = '...')
  2.   unpacked = text.unpack('U*')
  3.   unpacked.length > len ? unpacked[0..len-1].pack('U*') + tail : text
  4. end


2006/11/29 18:05 2006/11/29 18:05