Solved

Remove Hyperlinks From String

Posted on 2009-04-05
19
751 Views
Last Modified: 2012-05-06
Hello I need to remove hyperlinks from a string but leave the text of the hyperlink. The hyperlinks look like this. I am using vb.net 1.1. The only thing left in this example should be "Tollgate". The url employeeid, period, assignmentid, planid, allocatorid and elementid are variables not fixed values. Thank you much for any help as I am pulling what hair I have left out on it.

<a id="lnkVolumeDetails" href="/program.Statements/Y2005/Ultrasound/Rep/RevenueDetail.aspx?employeeid=6705&amp;period=200902&amp;assignmentid=12110&amp;planid=296&amp;elementid=2359&amp;allocatorid=3334" style="font-size:10pt;">= Current QTD Volume</a></TD>
0
Comment
Question by:scorpion53061
  • 9
  • 4
  • 4
  • +1
19 Comments
 
LVL 7

Expert Comment

by:garethh86
ID: 24071456
Sorry, I mainly deal with vb.net so my C# is a little ropey, you should get the gist of this though, hope it helps!
// Count the number of chars to ">"
 
    // Count characters. White space is treated as a character
    System.Text.RegularExpressions.MatchCollection charColl = System.Text.RegularExpressions.Regex.Matches(strOriginal, @">");
    strCount = charColl.Count.ToString();
 
// Removes specified number of characters(from the count before) starting at index 0
    strModified = strOriginal.Remove(0,strCount);
    MessageBox.Show(strModified);

Open in new window

0
 

Expert Comment

by:Scanbox
ID: 24071460
0
 
LVL 7

Assisted Solution

by:garethh86
garethh86 earned 100 total points
ID: 24071475
Sorry, remove the </a> aswell
string strNew = strModified.Replace("</a>", "");

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Expert Comment

by:Scanbox
ID: 24071476
sorry didnt read your text carefully enough
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 400 total points
ID: 24071609
In one line:
System.Text.RegularExpressions.Regex.Replace(url_string_object_here, "</?a[^>]*>", String.Empty)

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24071614
Ammendment:

Capture the output of the previous post in a variable as the Replace() function returns the new string.
0
 

Author Comment

by:scorpion53061
ID: 24071633
kaufmed are you saying I can put the whole string in there and it will remove the hyperlinks?
0
 

Author Comment

by:scorpion53061
ID: 24071636
I mean the whole HTML string (I mean there is going to be  more than one link that needs to be removed)
0
 
LVL 7

Expert Comment

by:garethh86
ID: 24071638
Much cleaner kaufmed, was trying to convert my vb code badly!
0
 
LVL 7

Expert Comment

by:garethh86
ID: 24071646
scorpion, where are the links coming from, is it from a databound item?
0
 

Author Comment

by:scorpion53061
ID: 24071647
I am in vb.net so that is cool.
0
 

Author Comment

by:scorpion53061
ID: 24071656
I put the HTML string in there and it didn't work. Not sure what I did wrong.
0
 

Author Comment

by:scorpion53061
ID: 24071667
no it is a string I am reading from a webpage.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24071999
I tested the RegEx and it removed all occurrences from my example. Perhaps if you post some code, we can better assist you.

My test code follows:
Application:
 
    Sub Main()
        Dim link As String = "<a id=""lnkVolumeDetails"" href=""/program.Statements/Y2005/Ultrasound/Rep/RevenueDetail.aspx?employeeid=6705&amp;period=200902&amp;assignmentid=12110&amp;planid=296&amp;elementid=2359&amp;allocatorid=3334"" style=""font-size:10pt;"">= Current QTD Volume</a></TD><a id=""lnkVolumeDetails"" href=""/program.Statements/Y2005/Ultrasound/Rep/RevenueDetail.aspx?employeeid=6705&amp;period=200902&amp;assignmentid=12110&amp;planid=296&amp;elementid=2359&amp;allocatorid=3334"" style=""font-size:10pt;"">= Current QTD Volume</a></TD><a id=""lnkVolumeDetails"" href=""/program.Statements/Y2005/Ultrasound/Rep/RevenueDetail.aspx?employeeid=6705&amp;period=200902&amp;assignmentid=12110&amp;planid=296&amp;elementid=2359&amp;allocatorid=3334"" style=""font-size:10pt;"">= Current QTD Volume</a></TD><a id=""lnkVolumeDetails"" href=""/program.Statements/Y2005/Ultrasound/Rep/RevenueDetail.aspx?employeeid=6705&amp;period=200902&amp;assignmentid=12110&amp;planid=296&amp;elementid=2359&amp;allocatorid=3334"" style=""font-size:10pt;"">= Current QTD Volume</a></TD><a id=""lnkVolumeDetails"" href=""/program.Statements/Y2005/Ultrasound/Rep/RevenueDetail.aspx?employeeid=6705&amp;period=200902&amp;assignmentid=12110&amp;planid=296&amp;elementid=2359&amp;allocatorid=3334"" style=""font-size:10pt;"">= Current QTD Volume</a></TD>"
        link = System.Text.RegularExpressions.Regex.Replace(link, "</?a[^>]*>", String.Empty)
 
        Console.WriteLine(link)
        Console.ReadKey()
    End Sub
 
Output:
 
= Current QTD Volume</TD>= Current QTD Volume</TD>= Current QTD Volume</TD>= Cur
rent QTD Volume</TD>= Current QTD Volume</TD>

Open in new window

0
 

Author Comment

by:scorpion53061
ID: 24072212
Okay this is the code that gives me the string from the web page? WIll that help?
Public Function EmailHttpContent(ByVal url As String) As String
        Dim pr As New System.Net.WebProxy("0.0.0.0", 88)
        Dim objRequest As WebRequest = System.Net.HttpWebRequest.Create(url)
        objRequest.Proxy = pr
        Dim sr As New StreamReader(objRequest.GetResponse().GetResponseStream())
        Dim result As String = sr.ReadToEnd()
        sr.Close()
        Return result
    End Function
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24074826
So something like the following doesn't work for you?
Public Function EmailHttpContent(ByVal url As String) As String
        Dim pr As New System.Net.WebProxy("0.0.0.0", 88)
        Dim objRequest As WebRequest = System.Net.HttpWebRequest.Create(url)
        objRequest.Proxy = pr
        Dim sr As New StreamReader(objRequest.GetResponse().GetResponseStream())
        Dim result As String = sr.ReadToEnd()
        sr.Close()
 
        Return System.Text.RegularExpressions.Regex.Replace(result, "</?a[^>]*>", String.Empty)
End Function

Open in new window

0
 

Author Comment

by:scorpion53061
ID: 24099959
I will have to bring in the function locally as it used elsewhere in the program without the need for what we are doing. But I will let you know.
Again thank you for your help.
0
 

Author Comment

by:scorpion53061
ID: 24099998
Holy crap that did work....youguys rock
0
 

Author Closing Comment

by:scorpion53061
ID: 31566751
outstanding people all around. patient and helpful.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

790 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