newx
2009-Sep-30 01:30 UTC
How to search and replace all urls on a HTML string using RUBY gsub
Hi ,
I trying to search and replace all urls on a HTML string
using gsub .
CODE
html = "<a href=''http://site.com.br''><img
src=''http://site3.com/
image.jpg''></a><a
href=''http://newx.com.br''><img
src=''http://localhost/
imagem.jpg''></a>";
pattern = /<a href=[\''"]?([^\''">
]*)[\''"]?[^>]*>(.*?)<\/a>/mo
replace = "<a
href=''http://mysite.com/redirect/#{$1}''>#{$2}</a>"
html_output = html.gsub(pattern,replace)
The REGEX pattern is apparently working but I ''m not getting the
values of $1 and $2 . When I use \\1 and \\2 it works .
Thing is ... I need to encode $1 variable like this
replace = "<a
href=''http://mysite.com/redirect/#{Base64.encode64
($1)}''>#{$2}</a>"
and I not able to encode \\1
Can anyone help me ?
Siddick Ebramsha
2009-Sep-30 07:07 UTC
Re: How to search and replace all urls on a HTML string using RUBY gsub
Try this one
html_output = html.gsub(pattern) do |link|
"<a
href=''http://mysite.com/redirect/#{$1}''>#{$2}</a>"
end
--
Posted via http://www.ruby-forum.com/.
newx
2009-Sep-30 13:36 UTC
Re: How to search and replace all urls on a HTML string using RUBY gsub
Hi Siddick ,
Thanks a lot for your response but ...
It didn''t work . html_output outputs the same content as
html variable .
Newton
On Sep 30, 4:07 am, Siddick Ebramsha <rails-mailing-l...@andreas-
s.net> wrote:> Try this one
>
> html_output = html.gsub(pattern) do |link|
> "<a
href=''http://mysite.com/redirect/#{$1}''>#{$2}</a>"
> end
>
> --
> Posted viahttp://www.ruby-forum.com/.
newx
2009-Sep-30 16:39 UTC
Re: How to search and replace all urls on a HTML string using RUBY gsub
Based in your solution and searching on internet I tryied the
following aproach and it did work this time .
pattern5 = /<a href=[\''"]?([^\''">
]*)[\''"]?[^>]*>(.*?)<\/a>/o
html.gsub!(pattern5) do |n|
"<a
href=''#{Base64.encode64($1)}''>#{$2}</a>"
end
puts html
I am now able to use $1 and $2
Thanks a lot .
Newton Garcia
Newx - Soluções para Internet
newx-P4ECzD7Nx0kIdKJ7tpkyPg@public.gmane.org
www.newx.com.br
On Sep 30, 10:36 am, newx
<newxh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hi Siddick ,
>
> Thanks a lot for your response but ...
> It didn''t work . html_output outputs the same content
as
> html variable .
>
> Newton
>
> On Sep 30, 4:07 am, Siddick Ebramsha <rails-mailing-l...@andreas-
>
> s.net> wrote:
> > Try this one
>
> > html_output = html.gsub(pattern) do |link|
> > "<a
href=''http://mysite.com/redirect/#{$1}''>#{$2}</a>"
> > end
>
> > --
> > Posted viahttp://www.ruby-forum.com/.