Remove a link from a string

Hi experts,

How can I remove the flickr from string width C#.

from exemple
test <a href="http://www.flickr.com/photos/22358585@N07/2194931828/" title="300atarot"><img src="http://static.flickr.com/2157/2194931828_ce0556b85f.jpg" alt="300atarot" border="0" /></a> test

must be changed to:
<img src="http://static.flickr.com/2157/2194931828_ce0556b85f.jpg" alt="300atarot" border="0" />

thanks
cabouAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
DhaestConnect With a Mentor Commented:
The easiest way: play with the string :)
    string test = " <a href='http://www.flickr.com/photos/22358585@N07/2194931828/' title='300atarot'><img src='http://static.flickr.com/2157/2194931828_ce0556b85f.jpg' alt='300atarot' border='0' /></a>"; 
    test = test.Substring(test.IndexOf("<im")); 

Open in new window

0
 
Bill-HansonCommented:
This function will extract the first IMG tag from a string containing HTML.
        public String getFirstImgTag(String html)
        {
            int start = html.IndexOf("<img");
            String part = html.Substring(start, html.Length - start);
            int end = part.IndexOf(">");
            return part.Substring(0, end+1);
        }

Open in new window

0
 
Bill-HansonConnect With a Mentor Commented:
Here's a better version of the function.  This one handles mixed-case tags and html with no IMG tag.
        public String getFirstImgTag(String html)
        {
            int start = html.ToLower().IndexOf("<img");
            if (start == -1) return "";
            String part = html.Substring(start, html.Length - start);
            int end = part.IndexOf(">");
            return part.Substring(0, end+1);
        }

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Bob LearnedCommented:
Totally cool way of HTML parsing and document processing.  *BIG GRIN*.  This class has much more possibilities than this small example can show, but it will achieve the desired result that you are looking for, even if it looks like overkill for that simple HTML example.

Bob
// Add a reference to Microsoft.mshtml to the project from the .NET tab.
 
using System;
using System.Collections.Generic;
 
/// <summary>
/// Process HTML text in order to achieve the desired results.  
///   1) Find element nodes by tag name (i.e. "a" = anchors, "img" = images, etc.)
///   2) Remove element nodes.
///   3) Add elements nodes.
///   4) Return document HTML text after processing.
/// </summary>
/// <remarks>
/// If additional processing is required, add additional methods, properties, and
/// events as desired to achieve the desired result.  
/// 
/// ***NOTE***
/// The Microsoft.mshtml library is very large, so avoid the temptation to include 
/// a using statement for the library, since it will have a dramatic impact on 
/// development time.
/// </remarks>
public class HtmlProcessor
{
 
	private mshtml.HTMLDocument _document = null;
 
	/// <summary>
	/// Constructor that takes as an argument the HTML text that needs
	/// to be processed.  The IHTMLDocument2 interface contains the correct
	/// form of the 'write' method to construct the HTMLDocument that has
	/// the methods needs to correctly parse HTML text.
	/// </summary>
	/// <param name="htmlText">The HTML text to process.</param>
	public HtmlProcessor(string htmlText)
	{
		mshtml.IHTMLDocument2 loader = (mshtml.IHTMLDocument2)new mshtml.HTMLDocument();
		loader.write(htmlText);
		_document = (mshtml.HTMLDocument)loader;
	}
 
	/// <summary>
	/// Return a list of elements nodes by tag name.  The IHTMLDOMNode interface
	/// has the necessary methods and properties for removing and appending 
	/// child nodes.
	/// </summary>
	/// <param name="tagName">The element tags to find (i.e. "a" = anchor).</param>
	/// <returns>A list of nodes based on tag name.</returns>
	public List<mshtml.IHTMLDOMNode> FindNodesByTagName(string tagName)
	{
		List<mshtml.IHTMLDOMNode> list = new List<mshtml.IHTMLDOMNode>();
 
		foreach (mshtml.IHTMLDOMNode node in _document.getElementsByTagName(tagName))
			list.Add(node);
 
		return list;
	}
 
	/// <summary>
	/// Append a child node to a parent node.
	/// </summary>
	/// <param name="parent">The parent node.</param>
	/// <param name="node">The child node.</param>
	public void AddNode(mshtml.IHTMLDOMNode parent, mshtml.IHTMLDOMNode child)
	{
		if (parent != null)
			parent.appendChild(child);
	}
 
	/// <summary>
	/// Remove a child node from it's parent.
	/// </summary>
	/// <param name="node">The node to remove.</param>
	public void RemoveNode(mshtml.IHTMLDOMNode node)
	{
		if (node.parentNode != null)
			node.parentNode.removeChild(node);
	}
 
	/// <summary>
	/// Return the document's main element as an IHTMLDOMNode.
	/// </summary>
	public mshtml.IHTMLDOMNode DocumentNode
	{
		get { return _document.documentElement as mshtml.IHTMLDOMNode; }
	}
 
	/// <summary>
	/// Return the resulting document's HTML text after processing is complete.
	/// </summary>
	public string DocumentHtml
	{
		get { return _document.documentElement.outerHTML; }
	}
 
}

Open in new window

0
 
vbturboConnect With a Mentor Commented:
try this

    public string ExamenString(string strIn) {
        string pattern = "href="(?<Link>.*?)";
        Regex Regex = new Regex(pattern);
        string strVal = Regex.Replace(strIn, "");
        return strVal;
    }

0
 
Bob LearnedConnect With a Mentor Commented:
Oops, forgot example code :(

Bob
string s = "test <a href=\"http://www.flickr.com/photos/22358585@N07/2194931828/\" title=\"300atarot\"><img src=\"http://static.flickr.com/2157/2194931828_ce0556b85f.jpg\" alt=\"300atarot\" border=\"0\" /></a> test";
 
HtmlProcessor parser = new HtmlProcessor(s);
List<mshtml.IHTMLDOMNode> images = parser.FindNodesByTagName("img");
List<mshtml.IHTMLDOMNode> anchors = parser.FindNodesByTagName("a");
 
parser.RemoveNode(anchors[0]);
parser.AddNode(parser.DocumentNode, images[0]);
 
string t = parser.DocumentHtml;

Open in new window

0
All Courses

From novice to tech pro — start learning today.