I'm putting together a web forum and want to parse web addresses that
are part of messages into clickable links. This is my attempt, I'm not
claiming its perfect but it works:
text.gsub!(/http:?\/*(\w+[\w\-\.\/~\?%&=#;,\+:\@]+)/,
' <a href="http://\1" target="_blank">http://\1</a>')
The problem is that some web addresses are incredibly long, so I want
the link text to be limited to, say, 50 chars. i.e. the second \1 needs
to be \1[0..49] or something. I can't figure out any easy way of doing
it, any ideas?
···
--
Posted via http://www.ruby-forum.com/.
$ ri gsub!
Implementation from String
···
On 7 August 2010 15:33, Bryan Kennerley <me@bryankennerley.co.uk> wrote:
I'm putting together a web forum and want to parse web addresses that
are part of messages into clickable links. This is my attempt, I'm not
claiming its perfect but it works:
text.gsub!(/http:?\/*(\w+[\w\-\.\/~\?%&=#;,\+:\@]+)/,
' <a href="http://\1" target="_blank">http://\1</a>')
The problem is that some web addresses are incredibly long, so I want
the link text to be limited to, say, 50 chars. i.e. the second \1 needs
to be \1[0..49] or something. I can't figure out any easy way of doing
it, any ideas?
--
Posted via http://www.ruby-forum.com/\.
------------------------------------------------------------------------------
str.gsub!(pattern, replacement) -> str or nil
str.gsub!(pattern) {|match| block } -> str or nil
str.gsub!(pattern) -> an_enumerator
------------------------------------------------------------------------------
Performs the substitutions of String#gsub in place, returning
str, or nil if no substitutions were performed. If no block and
no replacement is given, an enumerator is returned instead.
--
text.gsub(/http:?\/*(\w+[\w\-\.\/~\?%&=#;,\+:\@]+)/) { %Q{<a
href="http://\1" target="_blank">http://#{$1[0…50]}</a>} }
=> "<a href=\"http://\u0001"
target=\"_blank\">http://mydgfsjceeeefknuxqbkqkhdslkjdhxfilunhefilxqhleiufn</a>"
I am using %Q{str}, as "str" because you already have quotes. You
should also use %r{regex} for you Regexp, as it contains '/'.
Benoit Daloze
Missed the first '\1' :
text.gsub(/http:?\/*(\w+[\w\-\.\/~\?%&=#;,\+:\@]+)/) {|match| %Q{<a
href="http://#{$1}" target="_blank">http://#{$1[0…50]}</a>} }
···
On 7 August 2010 16:02:31 UTC+2, Benoit Daloze <eregontp@gmail.com> wrote:
On 7 August 2010 15:33, Bryan Kennerley <me@bryankennerley.co.uk> wrote:
I'm putting together a web forum and want to parse web addresses that
are part of messages into clickable links. This is my attempt, I'm not
claiming its perfect but it works:
text.gsub!(/http:?\/*(\w+[\w\-\.\/~\?%&=#;,\+:\@]+)/,
' <a href="http://\1" target="_blank">http://\1</a>')
The problem is that some web addresses are incredibly long, so I want
the link text to be limited to, say, 50 chars. i.e. the second \1 needs
to be \1[0..49] or something. I can't figure out any easy way of doing
it, any ideas?
--
Posted via http://www.ruby-forum.com/\.
$ ri gsub!
Implementation from String
------------------------------------------------------------------------------
str.gsub!(pattern, replacement) -> str or nil
str.gsub!(pattern) {|match| block } -> str or nil
str.gsub!(pattern) -> an_enumerator
------------------------------------------------------------------------------
Performs the substitutions of String#gsub in place, returning
str, or nil if no substitutions were performed. If no block and
no replacement is given, an enumerator is returned instead.
--
text.gsub(/http:?\/*(\w+[\w\-\.\/~\?%&=#;,\+:\@]+)/) { %Q{<a href="http://\1" target="_blank">http://#{$1[0…50]}</a>} }
I am using %Q{str}, as "str" because you already have quotes. You should also use %r{regex} for you Regexp, as it contains '/'.
Benoit Daloze