Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Search text string and convert links into web links

Posted on 1999-01-04
10
Medium Priority
?
183 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

688 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