Results 1 to 19 of 19

Thread: Replace via Trend

  1. #1

    Thread Starter
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134

    Replace via Trend

    I have to search for a string inside another string, and replace it. However, I must base this on a trend. This trend is (with the quotes!)

    "*.php*"

    I would like to change that to

    "http://www.mysite.com/*.php*"

    Basically, I'd like to find any instances of double-quoted strings containing php and add http://www.mysite.com/ into the start of the inside. Is there any good way to do this?
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  2. #2

    Thread Starter
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Hmm. This is probably a bad way to do this... anyway, my main goal is to replace relative links in a page with absolute ones (so I can download a page from a website but all of the links and pictures will still work).
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  3. #3
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    A simplistic approch:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const wType As String = "*.php* *.jpg* *.ico*"
    4.  
    5. Private Sub Form_Load()
    6.    Call mySearch("*.php*")
    7. End Sub
    8.  
    9. Private Sub mySearch(ByVal myFind As String)
    10.    If InStr(wType, myFind) <> 0 Then MsgBox "http://www.mysite.com/" & myFind
    11. End Sub

  4. #4

    Thread Starter
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Unfortunately, it will be a _massive_ string, and thus I don't think that will work.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  5. #5
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Well, you could write a Select Case statement, with all possible
    *.???*.

    However, how many extention types can there be on a Web page. Surly not hundreds?
    Last edited by Bruce Fox; Jan 4th, 2003 at 07:47 PM.

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Ok, I have an idea... I'll just knock up a demo.

  7. #7

    Thread Starter
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Hey, thanks I was writing my own replace function for this, but I can't think anymore (I need to relax and unwind, I think). If you want me to post it, I can.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  8. #8
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Nearly there

  9. #9
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Like:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const myRep As String = "http://www.mysite.com/"
    4.  
    5. Private Sub Form_Load()
    6. Dim myTestString As String: myTestString = "java bla bla *.php* bla endofline"
    7.  
    8. Dim fPos As Integer
    9.  
    10.    If InStr(myTestString, "*.") <> 0 Then
    11.       ' The prefix of an Extension exists in a Line
    12.       fPos = InStr(myTestString, "*.")
    13.          ' Second line of defence, does that match end with an "*"
    14.          If Mid$(myTestString, fPos + 5, 1) = "*" Then
    15.             MsgBox Left$(myTestString, fPos - 1) & myRep & Mid$(myTestString, fPos)
    16.          End If
    17.    End If
    18.  
    19. End Sub

  10. #10
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Ooops,

    You just wanted the returned URL appended with the extension right.

    If so:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const myRep As String = "http://www.mysite.com/"
    4.  
    5. Private Sub Form_Load()
    6. Dim myTestString As String: myTestString = "java bla bla *.jpg* bla endofline"
    7.  
    8. Dim fPos As Integer
    9.  
    10.    If InStr(myTestString, "*.") <> 0 Then
    11.       ' The prefix of an Extension exists in a Line
    12.       fPos = InStr(myTestString, "*.")
    13.          ' Second line of defence, does that match end with an "*"
    14.          If Mid$(myTestString, fPos + 5, 1) = "*" Then
    15.             ' Return the URL appended with the extension
    16.             MsgBox myRep & Mid$(myTestString, fPos, 6)
    17.          End If
    18.    End If
    19.  
    20. End Sub

  11. #11
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    And... as a Function;
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const myRep As String = "http://www.mysite.com/"
    4.  
    5. Private Sub Form_Load()
    6.    MsgBox myInsert("java bla bla *.php* bla endofline")
    7. End Sub
    8.  
    9. Private Function myInsert(ByVal sLine As String) As String
    10.    Dim fPos As Integer
    11.  
    12.    If InStr(sLine, "*.") <> 0 Then
    13.       ' The prefix of an Extension exists in a Line
    14.       fPos = InStr(sLine, "*.")
    15.          ' Second line of defence, does that match end with an "*"
    16.          If Mid$(sLine, fPos + 5, 1) = "*" Then
    17.             ' Return the URL appended with the extension
    18.             myInsert = myRep & Mid$(sLine, fPos, 6)
    19.          End If
    20.    End If
    21.  
    22. End Function
    Last edited by Bruce Fox; Jan 4th, 2003 at 08:39 PM.

  12. #12
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    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?
    Last edited by seaweed; Jan 4th, 2003 at 08:45 PM.
    ~seaweed

  13. #13
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Hey Seaweed,

    I think he's looking to replace all relative links in the text of an html page with absolute links.
    I agree.


    And that's what the Function will do (in theory )

    I don't think using Replace was an option due to the amount
    of extensions he was dealing with (see my original posts and suggestions).


    Well see where it goes


    Bruce.

  14. #14
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    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:
    1. Option Explicit
    2.  
    3. Private Const cSRC As String = "src="""
    4. Private Const cHREF As String = "href="""
    5. Private Const cHTTP As String = "http://"
    6. Private Const cWEBSITE As String = "http://www.mysite.com/"
    7.  
    8. Private Sub Form_Load()
    9.    MsgBox FixRelativeLinks("<img src=""images/logo.gif"" width=276 height=110 alt=""Google"">")
    10. End Sub
    11.  
    12. Private Function FixRelativeLinks(sOriginalHTML As String) As String
    13.     Dim nSrcPos As Integer
    14.     Dim nHrefPos As Integer
    15.     Dim sBeg As String
    16.     Dim sEnd As String
    17.     Dim sRetVal As String
    18.    
    19.     sRetVal = sOriginalHTML
    20.    
    21.     ' Replace all non-absolute src tags
    22.     nSrcPos = InStr(1, sRetVal, cSRC)
    23.     Do While nSrcPos > 0
    24.         ' Verify that it's a relative attribute
    25.         If Not Mid(sRetVal, nSrcPos + Len(cSRC), Len(cHTTP)) = cHTTP Then
    26.             ' Insert the website in the appropriate place
    27.             sBeg = Left(sRetVal, nSrcPos + Len(cSRC) - 1)
    28.             sEnd = Right(sRetVal, Len(sRetVal) - Len(sBeg))
    29.             sRetVal = sBeg & cWEBSITE & sEnd
    30.         End If
    31.        
    32.         ' Look for the next src tag
    33.         nSrcPos = InStr(nSrcPos + Len(cSRC), sRetVal, cSRC)
    34.     Loop
    35.    
    36.     ' Replace all non-absolute href tags
    37.     nHrefPos = InStr(1, sRetVal, cHREF)
    38.     Do While nHrefPos > 0
    39.         ' Verify that it's a relative attribute
    40.         If Not Mid(sRetVal, nHrefPos + Len(cHREF), Len(cHTTP)) = cHTTP Then
    41.             ' Insert the website in the appropriate place
    42.             sBeg = Left(sRetVal, nHrefPos + Len(cHREF) - 1)
    43.             sEnd = Right(sRetVal, Len(sRetVal) - Len(sBeg))
    44.             sRetVal = sBeg & cWEBSITE & sEnd
    45.         End If
    46.        
    47.         ' Look for the next href tag
    48.         nHrefPos = InStr(nHrefPos + Len(cHREF), sRetVal, cHREF)
    49.     Loop
    50.    
    51.     FixRelativeLinks = sRetVal
    52. End Function
    Last edited by seaweed; Jan 4th, 2003 at 09:23 PM.
    ~seaweed

  15. #15
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Hmm, your function doesn't work that way for me...

    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 ....

    I modified it cause I reread his original post.

    Anyhoo;
    VB Code:
    1. myInsert = Left$(sLine, fPos - 1) & myRep & Mid$(sLine, fPos)
    would do the trick.


    Cheers 'n' Beers
    Bruce.

  16. #16

    Thread Starter
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Bruce Fox and seaweed, you gods you. Thank you, thank you, thank you both! Is there any way to fix it so that you can have multiple things (like href=, src=, action=, etc.)?

    I've tune you into what I'm doing. Creating an IE-based browser that strips off the VBWire and ADs.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  17. #17

    Thread Starter
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Never mind Looked through your code again. Thanks
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  18. #18

    Thread Starter
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    I'm posting this through VBFBrowser
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  19. #19

    Thread Starter
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    On top of the world one day, crushed underneath its weight the next It appears that it won't be possible seeing as IE handles the links internally. However, I still have a couple of ideas.... oh well Thanks for your help though guys.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width