Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Harry 8 posts 78 karma points
    Dec 03, 2020 @ 12:14
    Harry
    0

    Removing opening anchor tag content

    Hi,

    I'm looking for a method to remove the start of an anchor tag "<a>" and it's href URL it contains from an Umbraco Property.

    E.g. text = blogItem.GetPropertyValue<string>("text");

    I want to remove all anchor tags from the string above as I am exporting as a csv file however, there are different anchor tags which contains different href URL content.

    I know I can perform

    text = blogItem.GetPropertyValue<string>("text").Replace("</a>", ""); 
    

    to remove the end anchor tags, I'm just unsure how to do it at the start.

    Any suggestions would be greatly appreciated.

    Thanks

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Dec 03, 2020 @ 13:18
    Huw Reddick
    0

    sounds like you probably need to use regex, could you give a few example anchor tags and what you want the outcome to be.

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Dec 03, 2020 @ 16:50
    Steve Morgan
    0

    Hi,

    I would use HtmlAgilityPack (install via Nuget).

    @using HtmlAgilityPack
    
    @{
      Layout = null;
    }
    @{
        var htmlDoc = new HtmlDocument();
        var text = blogItem.GetPropertyValue<string>("text").Replace("</a>", ""); 
    
        htmlDoc.LoadHtml(text);
    
        var aNodes = htmlDoc.DocumentNode.SelectNodes("//a");
    
        foreach (var node in aNodes)
        {
            // could check if they are an Umbraco link first here?
            node.Remove();
        }
        text = htmlDoc.DocumentNode.OuterHtml;
    }
    
    <textarea>@text</textarea>
    
Please Sign in or register to post replies

Write your reply to:

Draft