Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a URL entered in a text area as Hyperlink 5

Status
Not open for further replies.

Deadline

Programmer
Feb 28, 2001
367
US
Hello..
There is this text area, into which the users may enter some information along with a URL. The problem is,NOT only will they enter URL, but also with some other text...
This information will go to a table in the database.

I would like to ask help from you in writing a program for fetching the field and display the URL alone as a hyperlink in a page. The URL may be a lengthy one.

An example entry will be like :-
Code:
My Homepage is [URL unfurl="true"]http://communities.msn.com/iamnobillg[/URL]

Help me please. Thank you...
RR
 
TEKTIPS.ASP:


<%
dim cURL
cURL = rtrim(Request.Form(&quot;fURL&quot;))
if cURL<>&quot;&quot; then
Response.Write &quot;Try this link:<br>&quot;
Response.Write &quot;<a href=&quot;&quot; & cURL & &quot;&quot;&quot;>&quot; & cURL & &quot;</a>&quot;
end if

%>
<form name=frmTT action=tektips.asp method=post>
type=text name=fURL>
<input type=submit>
</form>
br
Gerard
 
Gerard,
Thank you very much.

Can you please repost the same, after enclosing the code
within the TGML 'code' tags ?
Thank you...
RR

 
it's the same:
Code:
<%
dim cURL
cURL = rtrim(Request.Form(&quot;fURL&quot;))
if cURL<>&quot;&quot; then
 Response.Write &quot;Try this link:<br>&quot;
 Response.Write &quot;<a href=&quot;&quot;[URL unfurl="true"]http://&quot;[/URL] & cURL & &quot;&quot;&quot;>&quot; & cURL & &quot;</a>&quot;
end if

%>
<form name=frmTT action=tektips.asp method=post>
[URL unfurl="true"]http://<input[/URL] type=text name=fURL>
<input type=submit>
</form>
br
Gerard
 
Gerard..
Many Thanks.

But my requirement is that, I need to be able to fish out a URL if Any from a text area and display it in the page.. The user will be entering a URL along with some other text information. My ASP page after saving all those info to the database, will have to display the URL alone as hyperlinked, while displaying the other stuff normally.

Its something like they do it here in Tek-tips.

For example if the user enters
Code:
 &quot;My Home page is [URL unfurl="true"]http://microsoft.com/bgates&quot;[/URL]
in that text area,he should be able to see

My Home page is
How to do it ? Can you help in this Gerard ? Many THanks Again...

Thank you...
RR
 
Code:
<%

if Request.ServerVariables(&quot;REQUEST_METHOD&quot;) = &quot;POST&quot; then
 dim cText, nStart, nEnd
 cText = lcase(Request.Form(&quot;fText&quot;))
 nStart = Instr(1, cText, &quot;[URL unfurl="true"]http://&quot;)[/URL]
 nEnd = Instr(nStart, cText,&quot; &quot;)
 if nStart<>0 then
  Response.Write &quot;I recognized this:<br>&quot;
  if nEnd = 0 then
   nEnd = len(cText) + 1
  end if
  Response.Write &quot;<a href=&quot;&quot;&quot; &_
   mid(cText,nStart,nEnd-nStart) & &quot;&quot;&quot;>&quot; &_
   mid(cText,nStart,nEnd-nStart) & &quot;</a>&quot;
 end if
end if

%>
<form name=frmTT action=tektips.asp method=post>
<textarea name=fText></textarea>
<input type=submit>
</form>
br
Gerard
 
Use regular expressions. Make sure that you have VBScript 5 in order to use them...

This will also use the match object, i'll try to comment the code well so that you can understand it.

there's more info at
Code:
dim re, matches, match, newstr
dim firstindex, fulllen
set re = new regexp
re.Global = true 'affect the entire string, not just the first one
re.IgnoreCase = true 'guess what this does :)
re.Pattern = &quot;([URL unfurl="true"]http://)?(\w+\.\w+)\.\w{3,4}&quot;[/URL] 'should fit most URL formats
set matches = re.Execute(txtAreaStr) 'get the matches collection

for each match in matches
 firstindex = match.FirstIndex 'the index of the first find
 fullLen = firstindex + match.Length 'the full length
 if (instr(match.value,&quot;[URL unfurl="true"]http://&quot;)[/URL] < 1) then 'look for the http... add if needed
  txtAreaStr = left(txtAreaStr, firstindex) & &quot;<a href=&quot;&quot;&quot; & _
   match.Value & &quot;&quot;&quot;>&quot; & match.Value & &quot;</a>&quot; & right(txtAreaStr, len(txtAreaStr) - (fullLen))
 else 'no http found, so tack it on
  txtAreaStr = left(txtAreaStr, firstindex) & &quot;<a href=&quot;&quot;[URL unfurl="true"]http://&quot;[/URL] & _
  match.Value & &quot;&quot;&quot;>&quot; & match.Value & &quot;</a>&quot; & right(txtAreaStr, len(txtAreaStr) - (fullLen))
 end if
next

I can't test this, so you might want to double check this. The regular expression matches any pattern like or xxx.yyy.com
it *should* also match any subdomain, and the 4 character TLD (.info). Right now, it doesn't adapt to things like .co.uk... but you should be able to add that fairly easily.

anyhow - above is the general idea of what you want to do... plus the code base to do it. Again - you're going to need to debug this code, since I don't know if I'm doing the string manipulation exactly correctly.

hth
leo hth
leo

------------
Leo Mendoza
lmendoza@students.depaul.edu
 
Leo and Gerard..
Thank you very much for your valuable time and help.

Leo, I have a doubt..Will it work if I use a lengthy URL like
Code:
[URL unfurl="true"]http://www.tek-tips.com/viewthread.cfm?spid=333&page=1&newpid=333&sqid=145121&CFID=23806181&CFToken=84331458[/URL]
?

How to handle such a situation ? Just like Tek-Tips does it so nicely ?

Gerard, I am going to try your code tomorrow morning. I'll let you know my progress.

Many Many Thanks again for your time...
Thank you...
RR
 
RR -
nice catch... the code I have won't find any params or anything. if you want to add it, just change the regexp.

it should look something like:

re.pattern = &quot;(
This should cover any URL querystring variables, and also cover the co.uk or .au kind of URLS. again - I can't check this, so you need to debug it before implementation...

if you end up changing the regexp, please post the new one, just so that if anyone does a search for something like this, they'll get the correct code.

hth
leo

------------
Leo Mendoza
lmendoza@students.depaul.edu
 
Hi, i'm also in the same situation where i wanna convert text links to hyperlinks.

vasah20, i tried using your code, but am not getting any results. Just a blank page. I tried assigning a value to txtAreaStr since all the string operations are occurring on this variable.

No results though. I put the following code in after the variables are declared:

txtAreaStr=request.form(&quot;fText&quot;)
fText is a field coming from a form on a different page.

Would appreciate any advice.
Thx.
 
txt=&quot;This is a beautiful day!&quot;
pos1=InStr(txt,&quot;http&quot;)
pos2=InStr(pos1,txt,&quot; &quot;,1)
tempstr=mid(txt,pos1,pos2-pos1)
response.write tempstr

will give the url, it checks the start position of http and gets the position of the first space after http hence giving you the whole url, with a bit of work you can check for commas, periods etc or where they may just enter www. Saturday 12.00
im6.gif
im2.gif
im2.gif
20.00
im5.gif
im4.gif
im7.gif
3.00am
im8.gif
Sunday [img http
 
Here is an edited version of vasoh20's post above, the regexp has been corrected to work better as well as some changes to the bottom function to keep track of how many characters we have added to the string length before adding the next match:
Code:
<%
Option Explicit

dim txtAreaStr
txtAreaStr = &quot;something something something else [URL unfurl="true"]http://66.56.100.100/emoticons/fart.gif[/URL] something else something else [URL unfurl="true"]http://www.tek-tips.com/[/URL] something else [URL unfurl="true"]http://www.somewhere.us/?var=1&var2=2[/URL] more space more space more space 123.223.21.1/mypage.asp?myvar=&myothervar=5&quot;

Response.Write &quot;<b>Before: </b><br>&quot; & txtAreaStr & &quot;<hr>&quot;

dim re, matches, match, newstr
dim firstindex, fulllen
set re = new regexp
re.Global = true 'affect the entire string, not just the first one
re.IgnoreCase = true 'guess what this does :)

re.Pattern = &quot;([URL unfurl="true"]http://)?((([/URL][a-zA-Z]+\.[\w-_]+)(\.[\w-_]+)*(\.\w{2,4}))|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))(/\w*)*(\.\w{3,4})?(\?(\w+=\w*)(\&\w+=\w*)?)?&quot;

set matches = re.Execute(txtAreaStr) 'get the matches collection

Response.Write &quot;<b>Matches: </b><br>&quot;

Dim charactersAdded		'Will count extra characters added like the <a href=, the http's, etc
charactersAdded = 0		'to keep the index correct
For Each match in matches
	Response.Write &quot;match: &quot; & match.value & &quot;<i> at index: &quot; & match.firstindex & &quot;</i><br>&quot;
	firstindex = match.FirstIndex + charactersAdded 'the index of the first find + previously added characters
	fullLen = firstindex + match.Length				'the full length
	if (instr(match.value,&quot;[URL unfurl="true"]http://&quot;)[/URL] < 1) then		'look for the http... add if needed
		txtAreaStr = left(txtAreaStr, firstindex) & &quot;<a href=&quot;&quot;&quot; & _
			match.Value & &quot;&quot;&quot;>&quot; & match.Value & &quot;</a>&quot; & _
			right(txtAreaStr, len(txtAreaStr) - (fullLen))
		charactersAdded = charactersAdded + 15 + match.Length
	else 'no http found, so tack it on
		txtAreaStr = left(txtAreaStr, firstindex) & &quot;<a href=&quot;&quot;[URL unfurl="true"]http://&quot;[/URL] & _
			match.Value & &quot;&quot;&quot;>&quot; & match.Value & &quot;</a>&quot; & _
			right(txtAreaStr, len(txtAreaStr) - (fullLen))
		charactersAdded = charactersAdded + 15 + 7 + match.Length
	end if
next 

Response.Write &quot;<hr><b>After: </b><br>&quot;
Response.Write txtAreaStr
%>

Thanks for a fun coffee break :)
-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
thx for the quick reply Gary.

I came across a great script though to take care of everything. Paste the following into a .asp file and open it. Its really good. Only drawback is that it doesn't recognize email addresses.

If Option Explicit is set on your pages, you'll have to declare the variables being used.

<%
Function LinkURLs(strInput)
iCurrentLocation = 1
Do While InStr(iCurrentLocation, strInput, &quot; 1) <> 0
iLinkStart = InStr(iCurrentLocation, strInput, &quot; 1)
iLinkEnd = InStr(iLinkStart, strInput, &quot; &quot;, 1)
If iLinkEnd = 0 Then iLinkEnd = Len(strInput) + 1
Select Case Mid(strInput, iLinkEnd - 1, 1)
Case &quot;.&quot;, &quot;!&quot;, &quot;?&quot;
iLinkEnd = iLinkEnd - 1
End Select
strOutput = strOutput & Mid(strInput, iCurrentLocation, iLinkStart - iCurrentLocation)
strLinkText = Mid(strInput, iLinkStart, iLinkEnd - iLinkStart)
strOutput = strOutput & &quot;<a href=&quot;&quot;&quot;&strLinkText&&quot;&quot;&quot;>&quot;&strLinkText&&quot;</a>&quot;
iCurrentLocation = iLinkEnd
Loop
strOutput = strOutput & Mid(strInput, iCurrentLocation)
LinkURLs = strOutput
End Function
strUnlinked = &quot; rules! <br>&quot; & vbCrLf
strUnlinked = strUnlinked & &quot; sells great computers!<br>&quot; & vbCrLf

' Here is the before text:
Response.Write &quot;<b>Original Text:</b><br>&quot; & vbCrLf
Response.Write strUnlinked
Response.Write vbCrLf & &quot;<br>&quot; & vbCrLf & vbCrLf

' Here is the text after it gets automatically hyperlinked to itself:
Response.Write &quot;<b>Text After Linking:</b><br>&quot; & vbCrLf
Response.Write LinkURLs(strUnlinked)
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top