Solved

Search text string and convert links into web links

Posted on 1999-01-04
10
180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel - Save a copy of work book 13 91
Hide vba in gp 7 116
I need help using System.Web.HttpUtility.HtmlEncode in my VB.Net application 3 111
Problem to With line 4 75
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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

733 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