|
it
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Using ASP to replace content but not tag content in HTML pages
Using ASP I'd like to modify a string that contains HTML. I need to modify the content (the bit that the users see) but not the stuff in the tags. For example, if I had the following string <a href=www.replacetext.com>replacetext.com</a> and wanted to replace "replacetext.com" with "newtext.com" I'd want the output to be <a href=www.replacetext.com>newtext.com</a> Could anyone help me with this? Thanks, Ben. "Sir Ben" wrote in message
Show quote news:LUkZd.21043$3A6.17418@newsfe1-gui.ntli.net... Ya', I could do that but looking at your email addy, does this mean I'm : Using ASP I'd like to modify a string that contains HTML. : : I need to modify the content (the bit that the users see) but not the stuff : in the tags. : : For example, if I had the following string : : <a href=www.replacetext.com>replacetext.com</a> : : and wanted to replace "replacetext.com" with "newtext.com" I'd want the : output to be : : <a href=www.replacetext.com>newtext.com</a> : : Could anyone help me with this? going to get some pr0n spam with disguised links? -- Roland Hall /* This information is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. */ Technet Script Center - http://www.microsoft.com/technet/scriptcenter/ WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp MSDN Library - http://msdn.microsoft.com/library/default.asp Roland,
Thanks for replying. No, you won't be getting any porn spam links as that would be very rude to someone who's helping me and you've given a oid email address anyway. Could you please post a solution on this newsgroup? Thanks, Sir Ben. Show quote "Roland Hall" <nobody@nowhere> wrote in message news:emh6GdMKFHA.3336@TK2MSFTNGP10.phx.gbl... > "Sir Ben" wrote in message > news:LUkZd.21043$3A6.17418@newsfe1-gui.ntli.net... > : Using ASP I'd like to modify a string that contains HTML. > : > : I need to modify the content (the bit that the users see) but not the > stuff > : in the tags. > : > : For example, if I had the following string > : > : <a href=www.replacetext.com>replacetext.com</a> > : > : and wanted to replace "replacetext.com" with "newtext.com" I'd want the > : output to be > : > : <a href=www.replacetext.com>newtext.com</a> > : > : Could anyone help me with this? > > Ya', I could do that but looking at your email addy, does this mean I'm > going to get some pr0n spam with disguised links? > > -- > Roland Hall > /* This information is distributed in the hope that it will be useful, but > without any warranty; without even the implied warranty of merchantability > or fitness for a particular purpose. */ > Technet Script Center - http://www.microsoft.com/technet/scriptcenter/ > WSH 5.6 Documentation - > http://msdn.microsoft.com/downloads/list/webdev.asp > MSDN Library - http://msdn.microsoft.com/library/default.asp > > "Sir Ben" wrote in message
news:eclZd.21059$3A6.16135@newsfe1-gui.ntli.net... ok, just a min... brb: Roland, : : Thanks for replying. : : No, you won't be getting any porn spam links as that would be very rude to : someone who's helping me and you've given a oid email address anyway. : : Could you please post a solution on this newsgroup? "Roland Hall" wrote in message
Show quote news:%23SZ60KNKFHA.2784@TK2MSFTNGP09.phx.gbl... Only gone 10 minutes. Everything's bigger in Texas! (O;=-: "Sir Ben" wrote in message : news:eclZd.21059$3A6.16135@newsfe1-gui.ntli.net... :: Roland, :: :: Thanks for replying. :: :: No, you won't be getting any porn spam links as that would be very rude to :: someone who's helping me and you've given a oid email address anyway. :: :: Could you please post a solution on this newsgroup? : : ok, just a min... brb I see you found something Sir Ben, a.k.a. Lazaroo... Here is what I came up with: <%@ Language=VBScript %> <% Option Explicit Response.Buffer = True function disguise(str, strR) dim re, col, strLeft, strRight set re = new RegExp with re .Pattern = "<[^>]*>" .Global = true .IgnoreCase = true end with set col = re.Execute(str) disguise = col(0) & strR & col(1) set col = nothing set re = nothing end function dim original, replacement, strNew original = "<a href=www.replacetext.com>replacetext.com</a>" replacement = "newtext.com" strNew = disguise(original, replacement) Response.Write "Original: " & Server.HTMLEncode(original) & "<br />" & Response.Write "New String: " & Server.HTMLEncode(strNew)vbCrLf Response.Write "Replacement: " & replacement & "<br />" & vbCrLf %> I encoded the strings so they would display properly in the example when you run it. They're not needed otherwise. You can see it working here: http://kiddanger.com/lab/htmlx2.asp HTH... (no spamming... you promised) -- Roland Hall /* This information is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. */ Technet Script Center - http://www.microsoft.com/technet/scriptcenter/ WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp MSDN Library - http://msdn.microsoft.com/library/default.asp I just noticed... this:
: dim re, col, strLeft, strRight should be:dim re, col ....only. Those two variables are no longer being used. -- Roland Hall /* This information is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. */ Technet Script Center - http://www.microsoft.com/technet/scriptcenter/ WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp MSDN Library - http://msdn.microsoft.com/library/default.asp Strangely enough I was thinking the same thing......
Show quote "Roland Hall" <nobody@nowhere> wrote in message news:emh6GdMKFHA.3336@TK2MSFTNGP10.phx.gbl... > "Sir Ben" wrote in message > news:LUkZd.21043$3A6.17418@newsfe1-gui.ntli.net... > : Using ASP I'd like to modify a string that contains HTML. > : > : I need to modify the content (the bit that the users see) but not the > stuff > : in the tags. > : > : For example, if I had the following string > : > : <a href=www.replacetext.com>replacetext.com</a> > : > : and wanted to replace "replacetext.com" with "newtext.com" I'd want the > : output to be > : > : <a href=www.replacetext.com>newtext.com</a> > : > : Could anyone help me with this? > > Ya', I could do that but looking at your email addy, does this mean I'm > going to get some pr0n spam with disguised links? > > -- > Roland Hall > /* This information is distributed in the hope that it will be useful, but > without any warranty; without even the implied warranty of merchantability > or fitness for a particular purpose. */ > Technet Script Center - http://www.microsoft.com/technet/scriptcenter/ > WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp > MSDN Library - http://msdn.microsoft.com/library/default.asp > > I feel this is most unfair. Especially as there's no way of me to SPAM you
anyway. Is there nothing I can do to convince you to help me? I've even changed my name now! "Steven Burn" <somewhere@in-time.invalid> wrote in message Strangely enough I was thinking the same thing......news:OMGpthMKFHA.2688@TK2MSFTNGP15.phx.gbl... Show quote "Roland Hall" <nobody@nowhere> wrote in message news:emh6GdMKFHA.3336@TK2MSFTNGP10.phx.gbl... > "Sir Ben" wrote in message > news:LUkZd.21043$3A6.17418@newsfe1-gui.ntli.net... > : Using ASP I'd like to modify a string that contains HTML. > : > : I need to modify the content (the bit that the users see) but not the > stuff > : in the tags. > : > : For example, if I had the following string > : > : <a href=www.replacetext.com>replacetext.com</a> > : > : and wanted to replace "replacetext.com" with "newtext.com" I'd want the > : output to be > : > : <a href=www.replacetext.com>newtext.com</a> > : > : Could anyone help me with this? > > Ya', I could do that but looking at your email addy, does this mean I'm > going to get some pr0n spam with disguised links? > > -- > Roland Hall > /* This information is distributed in the hope that it will be useful, but > without any warranty; without even the implied warranty of merchantability > or fitness for a particular purpose. */ > Technet Script Center - http://www.microsoft.com/technet/scriptcenter/ > WSH 5.6 Documentation - > http://msdn.microsoft.com/downloads/list/webdev.asp > MSDN Library - http://msdn.microsoft.com/library/default.asp > > Bit sloppy but, see if this helps;
sSource = "<a href=""somewebsite.com"">somewebsite.com</a>" sReplaceWhat = "somewebsite.com" sReplaceWith = "someotherwebsite.com" Response.Write Replace(sSource, ">" & sReplaceWhat & "<", ">" & sReplaceWith & "<") There's an example here;http://mysteryfcm.plus.com/misc/srep.asp Show quote "Lazaroo" <j***@financialcharting.com> wrote in message news:dDlZd.21162$3A6.21033@newsfe1-gui.ntli.net... > I feel this is most unfair. Especially as there's no way of me to SPAM you > anyway. > > Is there nothing I can do to convince you to help me? I've even changed my > name now! > "Steven Burn" <somewhere@in-time.invalid> wrote in message > news:OMGpthMKFHA.2688@TK2MSFTNGP15.phx.gbl... > Strangely enough I was thinking the same thing...... > > -- > Regards > > Steven Burn > Ur I.T. Mate Group > www.it-mate.co.uk > > Keeping it FREE! > > "Roland Hall" <nobody@nowhere> wrote in message > news:emh6GdMKFHA.3336@TK2MSFTNGP10.phx.gbl... > > "Sir Ben" wrote in message > > news:LUkZd.21043$3A6.17418@newsfe1-gui.ntli.net... > > : Using ASP I'd like to modify a string that contains HTML. > > : > > : I need to modify the content (the bit that the users see) but not the > > stuff > > : in the tags. > > : > > : For example, if I had the following string > > : > > : <a href=www.replacetext.com>replacetext.com</a> > > : > > : and wanted to replace "replacetext.com" with "newtext.com" I'd want the > > : output to be > > : > > : <a href=www.replacetext.com>newtext.com</a> > > : > > : Could anyone help me with this? > > > > Ya', I could do that but looking at your email addy, does this mean I'm > > going to get some pr0n spam with disguised links? > > > > -- > > Roland Hall > > /* This information is distributed in the hope that it will be useful, but > > without any warranty; without even the implied warranty of merchantability > > or fitness for a particular purpose. */ > > Technet Script Center - http://www.microsoft.com/technet/scriptcenter/ > > WSH 5.6 Documentation - > > http://msdn.microsoft.com/downloads/list/webdev.asp > > MSDN Library - http://msdn.microsoft.com/library/default.asp > > > > > > > I should mention, my "solution" is going to look extremely pitiful compared to Roland's (usually does <g>) so you may want to wait for his ;o)
Thanks for the help Steven!
I kept on searching and after 3 hours I finally got what I wanted!!!! It was on this site: http://coveryourasp.com/ShowFileDescr.asp And the code for anyone else interested is below... function htmlContentReplace(strHTML,strSearch,strReplace) htmlContentReplace = RegExpReplace(strHTML, "(?![^<]+>)" + strSearch + "(?![^<]+>)", strReplace) 'htmlContentReplace = RegExpReplace(strHTML, "(?![^<\[]+[>\]])" + strSearch + "(?![^\[<]+[>\]])", strReplace) end function Function RegExpReplace(strInput, strPattern, strReplace) ' Use <?> to indicate the match you wish to replace ' Create and setup several variables: Dim regEx, Match, Matches, Position, strReturn Position = 1 strReturn = "" ' Set up the regular expression: Set regEx = New RegExp regEx.Pattern = strPattern regEx.IgnoreCase = True regEx.Global = True ' Get all the matches for it: Set Matches = regEx.Execute(strInput) ' Go through the Matches collection ' and build the output string: For Each Match in Matches strReturn=strReturn & Mid(strInput, Position, Match.FirstIndex+1-Position) strReturn=strReturn & Replace(strReplace, "<?>", Match.Value) Position = Len(Match.Value) + Match.FirstIndex + 1 Next ' Add any text after the last match strReturn = strReturn & Mid(strInput, Position, Len(strInput)) RegExpReplace = strReturn End Function "Steven Burn" <somewhere@in-time.invalid> wrote in message I should mention, my "solution" is going to look extremely pitiful compared news:%2390dJPNKFHA.3500@TK2MSFTNGP14.phx.gbl... to Roland's (usually does <g>) so you may want to wait for his ;o) Your welcome....... glad to see you found something to suit your needs ;o)
Show quote "Lazaroo" <j***@financialcharting.com> wrote in message news:pGmZd.21368$3A6.1341@newsfe1-gui.ntli.net... > Thanks for the help Steven! > > I kept on searching and after 3 hours I finally got what I wanted!!!! > > It was on this site: > > http://coveryourasp.com/ShowFileDescr.asp > > And the code for anyone else interested is below... > > function htmlContentReplace(strHTML,strSearch,strReplace) > htmlContentReplace = RegExpReplace(strHTML, "(?![^<]+>)" + strSearch + > "(?![^<]+>)", strReplace) > 'htmlContentReplace = RegExpReplace(strHTML, "(?![^<\[]+[>\]])" + strSearch > + "(?![^\[<]+[>\]])", strReplace) > end function > > Function RegExpReplace(strInput, strPattern, strReplace) > ' Use <?> to indicate the match you wish to replace > ' Create and setup several variables: > Dim regEx, Match, Matches, Position, strReturn > Position = 1 > strReturn = "" > > ' Set up the regular expression: > Set regEx = New RegExp > regEx.Pattern = strPattern > regEx.IgnoreCase = True > regEx.Global = True > > ' Get all the matches for it: > Set Matches = regEx.Execute(strInput) > > ' Go through the Matches collection > ' and build the output string: > For Each Match in Matches > strReturn=strReturn & Mid(strInput, Position, > Match.FirstIndex+1-Position) > strReturn=strReturn & Replace(strReplace, "<?>", Match.Value) > Position = Len(Match.Value) + Match.FirstIndex + 1 > Next > > ' Add any text after the last match > strReturn = strReturn & Mid(strInput, Position, Len(strInput)) > RegExpReplace = strReturn > End Function > > > "Steven Burn" <somewhere@in-time.invalid> wrote in message > news:%2390dJPNKFHA.3500@TK2MSFTNGP14.phx.gbl... > I should mention, my "solution" is going to look extremely pitiful compared > to Roland's (usually does <g>) so you may want to wait for his ;o) > > -- > Regards > > Steven Burn > Ur I.T. Mate Group > www.it-mate.co.uk > > Keeping it FREE! > > "Steven Burn" wrote in message
news:%2390dJPNKFHA.3500@TK2MSFTNGP14.phx.gbl... I should mention, my "solution" is going to look extremely pitiful compared to Roland's (usually does <g>) so you may want to wait for his ;o) Oh puhleease. I often have to pull out my reference material to read your code. I've even sent some of it out to be analyzed at HowdTheyDoThat.com and the response is always the same: "We're as baffled as you are. This guy's IQ must be off the chart!" -- Roland Hall /* This information is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. */ Technet Script Center - http://www.microsoft.com/technet/scriptcenter/ WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp MSDN Library - http://msdn.microsoft.com/library/default.asp ROFLMFAO!
Show quote "Roland Hall" <nobody@nowhere> wrote in message news:OXj6P3SKFHA.2852@TK2MSFTNGP14.phx.gbl... > "Steven Burn" wrote in message > news:%2390dJPNKFHA.3500@TK2MSFTNGP14.phx.gbl... > I should mention, my "solution" is going to look extremely pitiful compared > to Roland's (usually does <g>) so you may want to wait for his ;o) > > Oh puhleease. I often have to pull out my reference material to read your > code. I've even sent some of it out to be analyzed at HowdTheyDoThat.com > and the response is always the same: > > "We're as baffled as you are. This guy's IQ must be off the chart!" > > -- > Roland Hall > /* This information is distributed in the hope that it will be useful, but > without any warranty; without even the implied warranty of merchantability > or fitness for a particular purpose. */ > Technet Script Center - http://www.microsoft.com/technet/scriptcenter/ > WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp > MSDN Library - http://msdn.microsoft.com/library/default.asp > > |
|||||||||||||||||||||||