Solved

Remove Hyperlinks From String

Posted on 2009-04-05
19
746 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
 

Expert Comment

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

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 74

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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 74

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 74

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now