Robert Klemme
9/11/2007 1:36:00 PM
2007/9/11, Lee Jarvis <jarvo88@gmail.com>:
> Thanks that worked well, And no its not single chars, Which is the only
> reason i'm doing it this way..
>
> I have to split on whitespace (/ /) because spliting on characters would
> obviously split the text i want to transform, which means it wont match
> if the characters are trailing another word, HTML special chars for
> example
>
> h = {"~" => "~"}
>
> "hmm ~'.split(/ /).map{|c| h[c] || c}.join(' ')
>
> Outputs hmm ~, but obviously doing things like question marks wont work,
> Maybe i'll have to use loops and string#tr
I'd rather not do the split step, IMHO direct replacement will be faster:
h = {"#126" => "~"}
s.gsub(/&([^;]+);/) {|c| h[c] || "&#{c};"}
Btw, I believe there are standard classes that do this type of
replacement (entities in HTML documents) - maybe it's in CGI.
Kind regards
robert