Im not sure that's what he wants...
I think he's looking to replace all relative links in the text of an html page with absolute links.
For example, I just copied this bit from Google's site, which puts their logo at the top of the page:
<img src="images/logo.gif" width=276 height=110 alt="Google">
When I do a "View Source" on their page and save it to my local drive, then try to look at the page locally, the picture doesn't appear because it is a relative link.
To fix this, I would have to insert "http://www.google.com/" at the beginning of the src attribute, like so:
<img src="http://www.google.com/images/logo.gif" width=276 height=110 alt="Google">
What i don't understand is why the *.php* stuff? Wouldn't it be good enough to replace src=" with src="http://www.mysite.com/ (and also replace href=" with href="http://www.mysite.com/)?
Or am I way off here?
Hmm, your function doesn't work that way for me...
I tried your function, passing in the string" "<img src=""images/logo.*.php*"" width=276 height=110 alt=""Google"">" (I put the *.php* in there so it would get recognized as a place to insert text), but all it returned was:
http://www.mysite.com/*.php*
It cut off the beginning and ending attibutes for the tag (img, width, height)
The way I see it, something like this might work better: You pass in the string that represents the page, and all src= and href= tags that aren't absolute (don't start with http://) have "http://www.mysite.com/" inserted after the first quote.
Here's a working example (I used some constants at the top, which can be changed without having to rewrite any of the function code):
VB Code:
Option Explicit
Private Const cSRC As String = "src="""
Private Const cHREF As String = "href="""
Private Const cHTTP As String = "http://"
Private Const cWEBSITE As String = "http://www.mysite.com/"
Private Sub Form_Load()
MsgBox FixRelativeLinks("<img src=""images/logo.gif"" width=276 height=110 alt=""Google"">")
End Sub
Private Function FixRelativeLinks(sOriginalHTML As String) As String
Dim nSrcPos As Integer
Dim nHrefPos As Integer
Dim sBeg As String
Dim sEnd As String
Dim sRetVal As String
sRetVal = sOriginalHTML
' Replace all non-absolute src tags
nSrcPos = InStr(1, sRetVal, cSRC)
Do While nSrcPos > 0
' Verify that it's a relative attribute
If Not Mid(sRetVal, nSrcPos + Len(cSRC), Len(cHTTP)) = cHTTP Then
' Insert the website in the appropriate place
sBeg = Left(sRetVal, nSrcPos + Len(cSRC) - 1)
sEnd = Right(sRetVal, Len(sRetVal) - Len(sBeg))
sRetVal = sBeg & cWEBSITE & sEnd
End If
' Look for the next src tag
nSrcPos = InStr(nSrcPos + Len(cSRC), sRetVal, cSRC)
Loop
' Replace all non-absolute href tags
nHrefPos = InStr(1, sRetVal, cHREF)
Do While nHrefPos > 0
' Verify that it's a relative attribute
If Not Mid(sRetVal, nHrefPos + Len(cHREF), Len(cHTTP)) = cHTTP Then
' Insert the website in the appropriate place
sBeg = Left(sRetVal, nHrefPos + Len(cHREF) - 1)
sEnd = Right(sRetVal, Len(sRetVal) - Len(sBeg))
sRetVal = sBeg & cWEBSITE & sEnd
End If
' Look for the next href tag
nHrefPos = InStr(nHrefPos + Len(cHREF), sRetVal, cHREF)
Loop
FixRelativeLinks = sRetVal
End Function
Re: Hmm, your function doesn't work that way for me...
Quote:
Originally posted by seaweed
I tried your function, passing in the string" "<img src=""images/logo.*.php*"" width=276 height=110 alt=""Google"">" (I put the *.php* in there so it would get recognized as a place to insert text), but all it returned was:
http://www.mysite.com/*.php*
It cut off the beginning and ending attibutes for the tag (img, width, height)
My first Sub didn't do that :D....
I modified it cause I reread his original post.
Anyhoo;
VB Code:
myInsert = Left$(sLine, fPos - 1) & myRep & Mid$(sLine, fPos)
would do the trick.
Cheers 'n' Beers
Bruce.