How to get rid of non-ascii characters in ruby

后端 未结 7 1065
遥遥无期
遥遥无期 2020-11-30 18:55

I have a Ruby CGI (not rails) that picks photos and captions from a web form. My users are very keen on using smart quotes and ligatures, they are pasting from other sources

7条回答
  •  醉酒成梦
    2020-11-30 19:39

    Quick GS revealed this discussion which suggests the following method:

    class String
      def remove_nonascii(replacement)
        n=self.split("")
        self.slice!(0..self.size)
        n.each { |b|
         if b[0].to_i< 33 || b[0].to_i>127 then
           self.concat(replacement)
         else
           self.concat(b)
         end
        }
        self.to_s
      end
    end
    

提交回复
热议问题