Retrieve only image tag from TinyMCE editor in ASP.Net using RegularExpression in C# and VB.Net

Last Reply 2 months ago By kalpesh

Posted 2 months ago

Dear Sir/Madam,

i am using the tinymce editor and i am saving the content in db.

its working fine.

but the problem is that how can i hide all the html tags(of the content data) leaving the image tag on page load.

is that possible to remove all html tags leaving one html tag.

thanks for your help in advance.

 

 

Posted 2 months ago Modified on 2 months ago

You cannot hide all the tag forTinyMCE Editor. But you can get only image tag content onces you are saving the content.

RichTextBox Example using Free TinyMCE Editor in ASP.Net

Refer the below sample code for your reference.

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>TinyMCE ASP.Net Example</title>
    <script type="text/javascript" src="tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
    <script type="text/javascript">
    tinyMCE.init({
        mode: "textareas",
        theme: "advanced",
        plugins: "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager,filemanager",       
        theme_advanced_buttons2: "image",
       theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
 
    });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="RichTextBox" runat="server" TextMode="MultiLine"></asp:TextBox>
        <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
        <asp:Label ID="lblDisplay" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

C#

protected void btnSave_Click(object sender, EventArgs e)
{
    lblDisplay.Visible = true;
    RichTextBox.Visible = false;
    List<string> imagestring = GetImagesInHTMLString(RichTextBox.Text);
    foreach (string imagetag in imagestring)
    {
        lblDisplay.Text += imagetag + "<br/>";
    }
}
private List<string> GetImagesInHTMLString(string htmlString)
{
    List<string> images = new List<string>();
    string pattern = @"<(img)\b[^>]*>";

    Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
    MatchCollection matches = rgx.Matches(htmlString);

    for (int i = 0, i< matches.Count;i++)
    {
        images.Add(matches[i].Value);
    }

    return images;
}

VB.Net

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)
    lblDisplay.Visible = True
    RichTextBox.Visible = False
    Dim imagestring As List(Of String) = GetImagesInHTMLString(RichTextBox.Text)
    For Each imagetag As String In imagestring
        lblDisplay.Text += imagetag & "<br/>"
    Next
End Sub

Private Function GetImagesInHTMLString(ByVal htmlString As String) As List(Of String)
    Dim images As List(Of String) = New List(Of String)()
    Dim pattern As String = "<(img)\b[^>]*>"
    Dim rgx As Regex = New Regex(pattern, RegexOptions.IgnoreCase)
    Dim matches As MatchCollection = rgx.Matches(htmlString)
    Dim i As Integer = 0
    Do While (i < matches.Count)
      images.Add(matches(i).Value)
      i = (i + 1)
    Loop

    Return images
End Function

Screenshot

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html