Solved

Search text string and convert links into web links

Posted on 1999-01-04
10
179 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel VBA combine two working workbooks 8 60
Windows 10 start screen issues 9 55
MS Access - Capture pressed key onclick 4 41
Determine Range to Select 5 52
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

856 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