Solved

Search text string and convert links into web links

Posted on 1999-01-04
10
174 Views
Last Modified: 2010-05-03
I have an ASP project where I need this:

A function that will search an entire string and put <A HREF=xxx> link </A> around every word that starts with "http://" and ends with a non web character such as /,.)("
0
Comment
Question by:jfl
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 1

Expert Comment

by:mmahdi
Comment Utility
jfl,

Here is the function.
If I understood you right, this function will add <A HREF=XXX> at the beginning of the URL, providing the URL starts with 'HTTP://' and ends with characters not between (A - Z) or (a -z) or (0 - 9), if I got the logic wrong the following can be easily altered.

Function Make_Asp(UrlString As String) As String

'This function returns a blank string if the criteria is not met

Dim tmpstr As String
tmpstr = UCase(UrlString)
If InStr(1, tmpstr, "HTTP://") <> 0 Then
Select Case Right(tmpstr, 1)
Case Chr(65) To Chr(90), Chr(97) To Chr(122), Chr(48) To Chr(57)
  tmpstr = ""
Case Else
  tmpstr = "<A HREF=xxx>" & tmpstr & "</A>"
End Select
End If

Make_Asp = tmpstr

End Function


'to call the function you can do:

Dim x as String
x = Make_Asp(MyURL)

I hope this works

Regards
MMAHDI

0
 
LVL 3

Expert Comment

by:vikiing
Comment Utility
Mmhadi:

There's a tiny problem in this instruction:

   If InStr(1, tmpstr, "HTTP://") <> 0 Then

If TmpStr has "HTTP://" into itself BUT NOT AT THE BEGINNING, your "if" will return (wrongly) True, because the so found position (what Instr returns) will be greater than 0.

That If should read like this:

   If Left$(TmpStr,7) = "HTTP://"  then  <the rest>

In this case, only when line starts with "HTTP://", then the "then" branch will be executed.

0
 
LVL 1

Expert Comment

by:mmahdi
Comment Utility
Viiking,

Well spotted, Instr function returns the number of times a particular string exists withing another string. I just thought how many URL's have HTTP:// not @ the beginning, but you are right.


Regards
MMAHDI
0
 
LVL 1

Expert Comment

by:mmahdi
Comment Utility
Jfl,

Viiking made a valid point, this is your revised function:

Function Make_Asp(UrlString As String) As String

'This function returns a blank string if the criteria is not met

Dim tmpstr As String
tmpstr = UCase(UrlString)
If Ucase(Left(tmpstr, 7)) = "HTTP://" Then
Select Case Right(tmpstr, 1)
Case Chr(65) To Chr(90), Chr(97) To Chr(122), Chr(48) To Chr(57)
  tmpstr = ""
Case Else
  tmpstr = "<A HREF=xxx>" & tmpstr & "</A>"
End Select
End If

Make_Asp = tmpstr

End Function


'to call the function you can do:
Dim x as String
x = Make_Asp(MyURL)

I hope this works
MMAHDI
0
 

Author Comment

by:jfl
Comment Utility
That's very nice.

But the function must search a string. And that string doesn't only contain links, so if there are no links it should not return a blank. Also the string can contain more than 1 link and a lot of text.

This string could look like this:
"The car can be seen at http://www.car.com/"
which should look like this after the string has been through the function:
"The car can be seen at <A HREF=http://www.car.com/>http://www.car.com/</A>"
or like this:
"The site http://www.directmailer.dk/ is very cool also http://directmailer.dk works !"
which should look like this:
"The site <A HREF=http://www.directmailer.dk/>http://www.directmailer.dk/</A> is very cool also <A HREF=http://directmailer.dk/>http://directmailer.dk</A> works !"

I hope you can help!
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 2

Expert Comment

by:ginde
Comment Utility
It looks like a just a character substitution work.  It's not worth writing program for that. On Unix you have a tool called sed. If you do not have access to Unix, You can just download a it for free.
You can run it as
sed -e "s/http:\/\//<A HREF=xxx> link </A>/g" test.html
It should do your work.

0
 
LVL 3

Expert Comment

by:vikiing
Comment Utility
Friend Mmhadi: you've stated "Instr function returns the number of times a particular string exists".

I'm sorry to tell that you're wrong. Instr returns THE FIRST position where a string exists into another one (or zero if string can not be found).

Instr("ABCABC","C") will return 3, because "C" is at third position, and not 2 for "C" being twice there.

0
 
LVL 1

Expert Comment

by:MAVERICK
Comment Utility
I had a application to search out bookmarks.. And add the URLs...

The instr function will do the task if programmed correctly...

I'll find a quick example for you

0
 
LVL 1

Accepted Solution

by:
mmahdi earned 50 total points
Comment Utility
Ffl,

Alright I hope this will work for you, so far it has worked for me.


Public Function Make_Asp(MyText As String) As String

Dim tmpstr1 As String
Dim tmpstr2 As String
Dim startpos As Integer
Dim myasp As String

begin:
If InStr(1, MyText, "http://") > 0 Then
startpos = InStr(1, MyText, "http://")
tmpstr1 = Left(MyText, InStr(1, MyText, "http://") - 1)
For x = startpos To Len(MyText)
Counter = Counter + 1
tmpstr2 = Mid(MyText, x, 1)
If tmpstr2 = " " Or x = Len(MyText) Then
If x = Len(MyText) Then
    tmpstr2 = Mid(MyText, startpos, Counter)
Else
    tmpstr2 = Mid(MyText, startpos, Counter - 1)
End If
myasp = myasp & tmpstr1 & "<A HREF=" & tmpstr2 & ">" & tmpstr2 & "</A>"
Counter = 0
If Len(tmpstr1 & tmpstr2) < Len(MyText) Then
MyText = Mid(MyText, Len(tmpstr1) + Len(tmpstr2) + 1)
If InStr(1, MyText, "http://") = 0 Then
    myasp = myasp & MyText
Else
    GoTo begin
End If
End If
Exit For
End If
Next
Else
    myasp = MyText
End If
Make_Asp = myasp
       
       
End Function


'To call the function you do
'dim result as string
'result = Make_Asp (SomeText_Here)

Regards
MMAHDI
0
 
LVL 1

Expert Comment

by:MAVERICK
Comment Utility
I have found the code for the extracting the URLs...
 if mmahdi's code is unsuitable... just leave a comment or email tomcat203@geocities.com

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now