Solved

convert links to html function

Posted on 2002-03-12
26
241 Views
Last Modified: 2008-02-01
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
Comment
Question by:SuperLeon
  • 11
  • 7
  • 5
  • +3
26 Comments
 
LVL 1

Author Comment

by:SuperLeon
ID: 6858483
apparently, this board doesn't do it that great either...

:-)
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6858606
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
 
LVL 1

Author Comment

by:SuperLeon
ID: 6858673
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6858752
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
 
LVL 1

Author Comment

by:SuperLeon
ID: 6858858
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
 
LVL 1

Author Comment

by:SuperLeon
ID: 6858859
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
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6858872
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
 
LVL 1

Author Comment

by:SuperLeon
ID: 6858898
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
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6858947
I see...

Forget what I said then.

Fritz the Blank
0
 
LVL 18

Expert Comment

by:bruno
ID: 6859685
Leon,

don't you mean to be checking for

https://


instead of

shttp://


???
0
 
LVL 1

Author Comment

by:SuperLeon
ID: 6859793
yup...i corrected that earlier...i'll update the question
0
 
LVL 18

Expert Comment

by:bruno
ID: 6859858
just making sure, you mentioned "shttp://" in one of your comments as well....

:-)
0
 
LVL 3

Expert Comment

by:meos
ID: 6860084
~~ interesting ...
0
 
LVL 1

Author Comment

by:SuperLeon
ID: 6861317
for anyone who's interested, i just modified the function a little bit so that it works a little better
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6861334
What is the nature of these modifications...

Fritz the Blank
0
 
LVL 1

Author Comment

by:SuperLeon
ID: 6861370
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
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6861455
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
 
LVL 1

Author Comment

by:SuperLeon
ID: 6861560
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
 
LVL 18

Expert Comment

by:bruno
ID: 6862356
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
 
LVL 1

Author Comment

by:SuperLeon
ID: 7006457
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
 
LVL 18

Expert Comment

by:bruno
ID: 7006534
>>100 points still available
to anyone who suggests a better way.


have you tried magic yet?


BRUNO
0
 
LVL 1

Author Comment

by:SuperLeon
ID: 7006539
....as long as the 'better way' proves to be better wiseguy...
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 7006804
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
 
LVL 18

Expert Comment

by:bruno
ID: 7006896
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
 
LVL 1

Expert Comment

by:madeline448
ID: 9326222
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
 

Accepted Solution

by:
YensidMod earned 0 total points
ID: 9390609
PAQed per request/recommendation & Points refunded

YensidMod
Community Support Moderator
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question