[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

convert links to html function

Hi All,

I just finished creating a function (see below) that takes a string and returns that same string in html with all the links in the string formatted as html links. For example, if www.yahoo.com was in the string, the function would change that to <a href='www.yahoo.com' target='a_top'> www.yahoo.com </a>. Kind of like what happens on this board. My question to you experts is: Am I dooing this correct (it seems to work fine)? If not, please suggest a better way.


Here's the function:

function encodeLinks(myStr)
 
  myStr = " " & myStr

  dim startPos, endLinkPos, myLink, ctl
     
  'put all strings to convert to links in array
  ' - first parameter is the string to convert
  ' - second parameter is flag to see if we need to append http:// prefix {T/F}
  dim changeToLink(2,1)
         
     
   changeToLink(0,0) = " http://"
   changeToLink(0,1) = false
     
   changeToLink(1,0) = " www."
   changeToLink(1,1) = true     'append http:// prefix
     
   changeToLink(2,0) = " https://"
   changeToLink(2,1) = false
         
  startPos = 1
  for ctl = 0 to ubound(changeToLink,1)
     
    do while startPos <> 0  'returns 0 if can't find any more links
      startPos = instr(startPos,myStr,changeToLink(ctl,0),1)
         
      if startPos > 0 then

        'find the end of the text beginning at that character
        endLinkPos = instr(startPos+1,myStr," ")
     
        if endLinkPos = 0 then
          endLinkPos = len(myStr)
        end if
               
        'return everything between those positions (this will be the link)
        myLink = lcase(trim(mid(myStr,startPos,endLinkPos-startPos+1)))
        if changeToLink(ctl,1) then
          myLink = "http://" & trim(myLink)
        end if
               
        'place [<a href=...] in string at appropriate position
        myStr = left(mystr, startPos) & "<a href='" & myLink & "' target='a_top'>" & myLink & "</a> " & right(mystr,len(myStr)-endLinkPos)
               
        'change startpos to reflect modified ending position
        startPos = startPos + len("<a href='") + len(myLink) + len("' target='a_top'>") + len(myLink) + len("</a> ")

      end if    
    loop
  startPos = 1    
  next
           
  encodeLinks = myStr
       
end function



Suggestions are greatly appreciated.

0
SuperLeon
Asked:
SuperLeon
  • 11
  • 7
  • 5
  • +3
1 Solution
 
SuperLeonAuthor Commented:
apparently, this board doesn't do it that great either...

:-)
0
 
fritz_the_blankCommented:
Okay, I tried your code by using a textbox to enter the string and a button to fire the code. It seems to give me exactly what I enter into the textbox.

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=vbscript>

function encodeLinks(myStr)
 dim startPos, endLinkPos, myLink, ctl
         
 dim changeToLink(2)
 changeToLink(0) = " http://"
 changeToLink(1) = " www."
 changeToLink(2) = " shttp://"
         
 for ctl = 0 to ubound(changeToLink)    
   startPos = 1
   do while startPos <> 0
     startPos = instr(startPos+1,myStr,changeToLink(ctl))
                   
     if startPos > 0 then

       'find the end of the text beginning at that character
       endLinkPos = instr(startPos+1,myStr," ")
       if endLinkPos = 0 then
         endLinkPos = len(myStr)
       end if
                       
       'return everything between those positions (this will be the link)
       myLink = trim(mid(myStr,startPos,endLinkPos-startPos+1))
       if ctl = 1 then
         myLink = "http://" & trim(myLink)
       end if
                       
       'place [<a href=...] in string at appropriate position
       myStr = left(mystr, startPos) & "<a href='" & myLink & "' target='a_top'>" & myLink & "</a> " & right(mystr,len(myStr)-endLinkPos)
                       
       'change startpos to reflect modified ending position
       startPos = endLinkPos + len("<a href='") + len(myLink) + len("' target='a_top'>") + len("</a>")
                             
     end if
   loop    
 next
  msgbox myStr    
 encodeLinks = myStr
         
end function


</SCRIPT>

</HEAD>
<BODY>
<INPUT type="text" id=text1 name=text1 >
<INPUT type="button" value="Button" id=button1 name=button1 onClick = encodeLinks(text1.value)>
<P>&nbsp;</P>

</BODY>
</HTML>

Fritz the Blank
0
 
SuperLeonAuthor Commented:
sorry...try this:


<script language=vbscript>

function encodeLinks(myStr)
  myStr = " " & myStr
  dim startPos, endLinkPos, myLink, ctl

  dim changeToLink(2)
  changeToLink(0) = " http://"
  changeToLink(1) = " www."
  changeToLink(2) = " shttp://"
     
  startPos = 1
  for ctl = 0 to ubound(changeToLink)
    do while startPos <> 0
      startPos = instr(startPos,myStr,changeToLink(ctl))
     
      if startPos > 0 then
        'find the end of the text beginning at that character
        endLinkPos = instr(startPos+1,myStr," ")
        if endLinkPos = 0 then
          endLinkPos = len(myStr)
        end if
     
        'return everything between those positions (this will be the link)
        myLink = trim(mid(myStr,startPos,endLinkPos-startPos+1))
        if ctl = 1 then
          myLink = "http://" & trim(myLink)
        end if
           
        'place [<a href=...] in string at appropriate position
        myStr = left(mystr, startPos) & "<a href='" & myLink & "' target='a_top'>" & myLink & "</a> " & right(mystr,len(myStr)-endLinkPos)
         
        'change startpos to reflect modified ending position
        startPos = endLinkPos + len("<a href='") + len(myLink) + len("' target='a_top'>") + len("</a>")
               
    end if    
    loop
  startPos = 1    
  next
 
  msgbox(trim(myStr))
  'encodeLinks = trim(myStr)
     
end function


</script>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
fritz_the_blankCommented:
Okay!

If I enter Yahoo.com, I get Yahoo.com back.

If I enter www.Yahoo.com, I get back what you are hoping for.

If I enter http://www.Yahoo.com, I get back what you are hoping for as well.

This is not a big issue, but if I enter WWW.yahoo.CoM, the mixed case persists in your link. It will work but look ugly.

Fritz the Blank
0
 
SuperLeonAuthor Commented:
yeah...i'm only checking for http://, www., and shttp://

can't possibly check for all possible link variations...yahoo.com, google.com, msn.com, etc. I guess i kinda could by looking for [.com, .net, .org, .edu, etc...] and then searching backwards for the next space...but i'm not gonna do it. the user will just have to input www. or http://

i thought about lcasing the whole thing too...come to think of it, i probably will. my server side code is already ugly as it is...no need to impose that ugliness on the client.

thanks for the tip
0
 
SuperLeonAuthor Commented:
yeah...i'm only checking for http://, www., and shttp://

can't possibly check for all possible link variations...yahoo.com, google.com, msn.com, etc. I guess i kinda could by looking for [.com, .net, .org, .edu, etc...] and then searching backwards for the next space...but i'm not gonna do it. the user will just have to input www. or http://

i thought about lcasing the whole thing too...come to think of it, i probably will. my server side code is already ugly as it is...no need to impose that ugliness on the client.

thanks for the tip
0
 
fritz_the_blankCommented:
I was thinking that you could have a prefix routing to check for the left(strURl, x) equalling www. or http:// and etc., and if none of the three were there, you could add it.

Fritz the Blank
0
 
SuperLeonAuthor Commented:
the function takes ANY elements within the string and converts them. for instance it would change:

click on this link: www.yahoo.com to go to Yahoo!

to

click on this link: <a href='http://www.yahoo.com' target='a_top'>www.yahoo.com</a> to go to Yahoo!

0
 
fritz_the_blankCommented:
I see...

Forget what I said then.

Fritz the Blank
0
 
brunoCommented:
Leon,

don't you mean to be checking for

https://


instead of

shttp://


???
0
 
SuperLeonAuthor Commented:
yup...i corrected that earlier...i'll update the question
0
 
brunoCommented:
just making sure, you mentioned "shttp://" in one of your comments as well....

:-)
0
 
meosCommented:
~~ interesting ...
0
 
SuperLeonAuthor Commented:
for anyone who's interested, i just modified the function a little bit so that it works a little better
0
 
fritz_the_blankCommented:
What is the nature of these modifications...

Fritz the Blank
0
 
SuperLeonAuthor Commented:
sorry:

> I added another parameter to the 'changeToLink' array that flags if http:// is needed as a prefix to the link (as is the case when converting www. to a link). this way, all you have to do to handle other strings is add them to the array.

> I also noticed and corrected a slight bug in my calculation of startPos as I was going through the loop
0
 
fritz_the_blankCommented:
What about our discussion regarding case? Perhaps you might want to add a subroutine that examines the string to see if it is all caps or all lowercase and then modify the string to fix it.

Here is something that I wrote in JavaScript, but you get the idea:

function properName(strField)
{
//This function adds capitals to each word in a string if the original text is
//all upper case or all lower case
     var bolMixedCase, strValue, strTempValue, arrName, strProperName
     bolMixedCase = true;
     strValue = eval('document.forms[0].' + strField + '.value');
     strTempValue = strValue.toUpperCase()
     if(strValue==strTempValue){
          bolMixedCase = false;
     }
     strTempValue = strValue.toLowerCase()
     if(strValue==strTempValue){
          bolMixedCase = false;
     }
     if(! bolMixedCase){
          arrName= new Array(strValue.length)
          for(intAt=0;intAt<strValue.length;intAt++){
               arrName[intAt] = strValue.substring(intAt,intAt + 1);
          }
          arrName[0] = arrName[0].toUpperCase()
          strProperName = arrName[0]
          for(i=1;i<arrName.length;i++){
               strTest = arrName[i-1];
               if(arrName[i-1].toUpperCase()<"A" || arrName[i-1].toUpperCase()>"Z"){
                    arrName[i] = arrName[i].toUpperCase();
               }else{
                    arrName[i] = arrName[i].toLowerCase();
               }
               strProperName = strProperName + arrName[i];
          }
          eval('document.forms[0].' + strField + '.value=strProperName');
     }
}
0
 
SuperLeonAuthor Commented:
oh (thanks for reminding me fritz)....the other thing I changed was the instr function. i set the last parameter in the instr function so that it doesn't care about matching case sensitivity [set it to 1]:

startPos = instr(startPos,myStr,changeToLink(ctl,0),1)

{the line right after the do while... statement)

that seems to solve the case problem. i also lcased the outputted link so that it displays cleanly.

do you think this function is the best way to accomplish this task? i'd love to see how the experts-exchange board does it if at all possible. i know that might be crossing the proprietary code boundary, but isn't code-sharing what this site is all about?

moderators...what do you think?
0
 
brunoCommented:
if you want the mods attention, you gotta go post a 0 point Q in the CS TA.

EE also runs on JSP...next time the site goes down, see if it is coughing up it's JSP code.

i think it is all done behind the scenes though....


BRUNO


0
 
SuperLeonAuthor Commented:
FYI: I've modified the function a little. Here's what I'm currently using. 100 points still available to anyone who suggests a better way.


function encodeLinks(el_str)
  el_str = " " & el_str
  el_str = replace(el_str,">", "> ")
  el_str = replace(el_str,"<", " <")
 
  dim el_startPos, el_endLinkPos, el_myLink, el_ctl
     
  'put all strings to convert to links in array
  ' - first parameter is the string to convert
  ' - second parameter is flag to see if we need to append http:// prefix {T/F}
  dim changeToLink(2,1)
         
     
   changeToLink(0,0) = " http://"
   changeToLink(0,1) = false
     
   changeToLink(1,0) = " www."
   changeToLink(1,1) = true     'append http:// prefix
     
   changeToLink(2,0) = " https://"
   changeToLink(2,1) = false
         
  el_startPos = 1
  for el_ctl = 0 to ubound(changeToLink,1)
     
    do while el_startPos <> 0  'returns 0 if can't find any more links
      el_startPos = instr(el_startPos,el_str,changeToLink(el_ctl,0),1)
         
      if el_startPos > 0 then

        'find the end of the text beginning at that character
        el_endLinkPos = instr(el_startPos+1,el_str," ")
     
        if el_endLinkPos = 0 then
          el_endLinkPos = len(el_str)
        end if
               
        'return everything between those positions (this will be the link)
        el_myLink = lcase(trim(mid(el_str,el_startPos,el_endLinkPos-el_startPos+1)))
        if changeToLink(el_ctl,1) then
          el_myLink = "http://" & trim(el_myLink)
        end if
               
        'place [<a href=...] in string at appropriate position
        el_str = left(el_str, el_startPos) & "<a href='" & el_myLink & "' target='a_top'>" & el_myLink & "</a> " & right(el_str,len(el_str)-el_endLinkPos)
               
        'change el_startPos to reflect modified ending position
        el_startPos = el_startPos + len("<a href='") + len(el_myLink) + len("' target='a_top'>") + len(el_myLink) + len("</a> ")

      end if    
    loop
  el_startPos = 1    
  next
           
  encodeLinks = trim(el_str)
       
end function
0
 
brunoCommented:
>>100 points still available
to anyone who suggests a better way.


have you tried magic yet?


BRUNO
0
 
SuperLeonAuthor Commented:
....as long as the 'better way' proves to be better wiseguy...
0
 
fritz_the_blankCommented:
SuperLeon,

Has there been any one else chipping in on this other than Bruno and I? I think that we may have already earned the 100, no?

Fritz the Blank

0
 
brunoCommented:
Fritz,

I don't think I've done anything to deserve them, should Leon decide to close the question out I think you have sole claim.


Leon,

Just so you are aware, because this Q is rather old at this point, it's probably pretty far down the list, as it's two months old, i think it's at least 10 pages in, and probably won't get any more attention.



BRUNO
0
 
madeline448Commented:
SuperLeon,

No comment has been added lately (more than a year now), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: PAQ/Refund

Please leave any comments here within seven days.

-- PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER! --

Thanks,

madeline448
EE Cleanup Volunteer
0
 
YensidModCommented:
PAQed per request/recommendation & Points refunded

YensidMod
Community Support Moderator
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 11
  • 7
  • 5
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now