Solved

Search text string and convert links into web links

Posted on 1999-01-04
10
175 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
ID: 1453739
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
ID: 1453740
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
ID: 1453741
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
ID: 1453742
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
ID: 1453743
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:ginde
ID: 1453744
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
ID: 1453745
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
ID: 1453746
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
ID: 1453747
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
ID: 1453748
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net 2008 2 56
VBA color chart bars 12 77
Excel Vlookup to move data back to source. 4 83
VB6 ListBox Question 4 32
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…

911 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

18 Experts available now in Live!

Get 1:1 Help Now