Solved

Parse text and create HTML links where appropriate

Posted on 2004-04-19
8
797 Views
Last Modified: 2009-12-16
I am looking for the following stored procedure:

  CREATE PROCEDURE spActivateLinks

     @text varchar(8000)

  AS
     
       -- all code would go here


  GO



-------------------

I simply want it to activate any potential HTML links that are contained in the text passed to it.
The following text would be parsed and converted to a link:


  * External sites - "http://", "news://", "ftp://" etc etc
  * Local Links - "/news" or "/news/" etc etc

-----------------

Alternatively, this can be achieved in C# but I would prefer it in SQL.
  *
0
Comment
Question by:smacca
8 Comments
 

Author Comment

by:smacca
ID: 10859017
I noticed that Expert Exchange already do exactly what I want to:

  * Notice how the "http://" and "ftp://" have been made into links.

That is EXACTLY what I want to achieve.

Cheers.
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 10859073
Does you text already have some html tags
like
<HTML>
<BODY>
your text here
</BODY>
</HTML>

??

Could you post a sample text for us to work on ?

Hilaire
0
 
LVL 26

Assisted Solution

by:Hilaire
Hilaire earned 200 total points
ID: 10859116
If you are in a.Net environment, I'm sure this could be achieved very easily using regular expressions in your C# app.
Using merely 2-3 lines of code.
Maybe you should give it a try ?

Hilaire
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 3

Expert Comment

by:jayrod
ID: 10859302
I agree with hilaire here. I'm sure it would be possible to do some sort of string replacement. however the problem is that it would be much more effecient and easier to use a language that supports reg expressions
0
 
LVL 34

Expert Comment

by:arbert
ID: 10859334
I agree with the above.  It's not totally clear what you're working with, but usually it's the client application that decides if a link is encoded as an HREF--not the database app....
0
 
LVL 26

Assisted Solution

by:Hilaire
Hilaire earned 200 total points
ID: 10859915
here's a link to single lined function that should do the job

http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=423

I guess you'll manage to update the code so that it handles FTP urls too ...

Hilaire
0
 
LVL 5

Accepted Solution

by:
a1x earned 300 total points
ID: 10860792
You can use SQL server string functions to do what you want.  It is tedious and verbose compared to regular expressions, but it can be done.

Here is a sample that will create a link:

DECLARE @text varchar(4000)

SET @text = 'Turn http://www.yahoo.com into a hyperlink'

DECLARE @StartIndex smallint
DECLARE @EndIndex smallint
DECLARE @URL varchar(4000)

SET @StartIndex = charindex('http:', @text)
SET @EndIndex = charindex(' ', @text, @StartIndex)
SET @URL = substring(@text, @StartIndex, @EndIndex - @StartIndex)

SELECT STUFF(@text, @StartIndex, @EndIndex - @StartIndex, '<a href="' + @URL + '">' + @URL + '</a>')

The sample is very limited.  It only handles one reference in the string.  You would have to add indexes to handle multiple instances of http in the string.  Also, it assumes that the reference ends with space, which may not be the case.  
0
 

Expert Comment

by:Mustangz
ID: 12381131
I was wondering, does anyone know how to do this in ASP?
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

791 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