ropey has asked for the wisdom of the Perl Monks concerning the following question:
Excuse the dodgy title, I really couldn't think how to describe the problem very accurately.
So basically what I am trying to do, is to modify the source of the html document to add some special links (they are actually used to allow easy translation of a web page in context)
I think this is easier explained in a few examples, so lets take the easy one as follows, lets say I have this text in the html source
<!-- headerStandard.siteFeedbackLink.label --!>Feedback<!-- headerStan +dard.siteFeedbackLink.label --!>
And lets say that I have a hash such as
%data = ('headerStandard.siteFeedbackLink.label' => 'Feedback Translat +ed');
So the html comments are used to map the key to the content and are available for me in the html. I want to replace whats in between the tags with the data equivalent AND add a special link AFTER it as so above example becomes...
So this example is easy, I just regex between the lines such asFeedback Translated<a href='foo'>foo</a>
my $heading = 'headerStandard.siteFeedbackLink.label'; $wp =~ s/\<\!-- $heading --\!>(.*)\<\!-- $heading --\!>/$data{$hea +ding}\<a href="foo"\>foo\</a>/gm;
So no problem, however my slow working brain needs some assistance with dealing when the contents I want to replace are nested inside tags already, for instance lets say the text is inside a <a href="bar"> such as
So i would want the text to be replaced inside the a tag, but the link to be outside the current href such as follows<a href="http://localhost:8585/deals/dealshome"><!-- headerStandard.de +alsTab.label --!>Deals<!-- headerStandard.dealsTab.label --!></a>
or inside alt tags for a image<a href="http://localhost:8585/deals/dealshome">Deals Modified</a><a h +ref="foo">foo</a>
<a href="bar"><img src="http://localhost" alt=" <!-- adTags.AdvertisementAltText --!>Advertisement<!-- adTags.Advertis +ementAltText --!> " /></a>
Again would want the text to be replaced between the comments, but add the link outside of the original a tag.
This scenario also applies to text within textfield, textareas, img tags etc etc.... SO I guess can anyone suggest a easy way to achieve what I am doing (replace the text within the comments and add the link outside any nested tags).... My regex skills are not particularly great but I am sure some of you may be able to assist or offer me a better way of doing this ?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex within html
by moritz (Cardinal) on Sep 08, 2008 at 12:17 UTC | |
by ropey (Hermit) on Sep 08, 2008 at 12:38 UTC | |
by moritz (Cardinal) on Sep 08, 2008 at 13:08 UTC | |
by Anonymous Monk on Sep 08, 2008 at 12:43 UTC | |
|
Re: Regex within html
by shmem (Chancellor) on Sep 08, 2008 at 14:00 UTC | |
by ropey (Hermit) on Sep 08, 2008 at 14:36 UTC | |
by user0869 (Initiate) on Sep 09, 2008 at 07:40 UTC |