Projekt-Anfragen: Tel: 07022/9319004 info@CodeDocu.de Software Entwicklung in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

Asp: HTML to Text

 

Html in Text umwandeln

 

Wenn man in Asp Net Core MVC im Server HTML in Text umwandeln möchte für die Weiterverarbeitung, dann steht nicht wie bei Javascript eine einfache Methode zur Verfügung.

Deshalb liegt hier eine HTML zu Text Umwandlungs-Datei und Klasse bei, mit welcher man aus HTML Code den reinen Text ermitteln kann.

Der C# Code kann nach belieben angepasst und verbessert werden, je nach Anwendung.

 

Code Beispiel

 

Die Umwandlung kann mit einer einfachen C# Methode ausgeführt werden.

string sText = HTML.HTML_to_Text(sHTML);

 

 

Beispiel:

Eingabe in HTML und HTML Ansicht im TextArea

 

Das Ergebnis nach Umwandlung ist reiner Text ohne Tags und Steuerzeichen

 

Der Aufruf erfolgt in Asp.Net Core MVC im Controller in der Postback Methode

        [HttpPost]

        public IActionResult demo_jQuery_01_post(IFormCollection formFields)

        {

            //-------------< demo_jQuery_01_post >-------------

            //< get form input >

            string sHTML = formFields["ctlEditor_Text"];

            //</ get form input >

 

            //< convert to Text >

            string sText = HTML.HTML_to_Text(sHTML);

            //</ convert to Text >

 

 

            //< load Data >

            ViewData["Text1"] = sText;

            //ViewData["Text2"] = sText;

            //</ load Data >

 

            //< out >

            return View("demo_jQuery_01");

            //</ out >

            //-------------</ demo_jQuery_01_post >-------------

        }

 

 

 

 

Man muss einfach eine Datei HTML.cs erstellen und den folgenden C# Code einfügen.

Anschliessend können die Methoden von überall wie im Beispiel aufgerufen werden.

 

C# Code

zum Umwandeln von HTML zu reinem Text

 

 

public static class HTML

{

    public static string HTML_to_Text(string sHTML)

        {

        //--------< HTML_to_Text() >--------

        //< remove blocks >

        sHTML = remove_script(sHTML);

        sHTML = remove_Head(sHTML);

        //</ remove blocks >

 

 

        //< remove Html-Tags >

        sHTML = remove_Tags(sHTML);

        //</ remove Html-Tags >

 

        //< remove Charaters >

        sHTML = remove_Control_Characters(sHTML);

        sHTML = remove_HTML_Characters(sHTML);

        sHTML = remove_Special_Characters(sHTML);

        sHTML = remove_Punctuation_Mark_Characters(sHTML);

        sHTML = remove_Brackets_Characters(sHTML);

        //</ remove Charaters >

 

        //< output >

        return sHTML;

        //</ output >

        //--------</ HTML_to_Text() >--------

    }

 

    public static string remove_HTML_Characters(string sHTML)

    {

        //--------< HTML_to_Text() >--------

        sHTML = sHTML.Replace("&gt;", " ");

        sHTML = sHTML.Replace("&lt;", " ");

        sHTML = sHTML.Replace("&nbsp;", " ");

        sHTML = sHTML.Replace("&gt;", " ");

 

        while (sHTML.IndexOf("  ") >= 0)

        { sHTML = sHTML.Replace("  ", " "); }

 

        //< output >

        return sHTML;

        //</ output >

        //--------</ HTML_to_Text() >--------

    }

 

    public static string remove_Special_Characters(string sHTML)

    {

        //--------< HTML_to_Text() >--------

        sHTML = sHTML.Replace("\\", " ");

        sHTML = sHTML.Replace("/", " ");

 

        while (sHTML.IndexOf("  ") >= 0)

        { sHTML = sHTML.Replace("  ", " "); }

 

        //< output >

        return sHTML;

        //</ output >

        //--------</ HTML_to_Text() >--------

    }

 

    public static string remove_Punctuation_Mark_Characters(string sHTML)

    {

        //--------< remove_Punctuation_Mark_Characters() >--------

        sHTML = sHTML.Replace(";", " ");

        sHTML = sHTML.Replace(".", " ");

        sHTML = sHTML.Replace(",", " ");

        sHTML = sHTML.Replace("'", " ");

        sHTML = sHTML.Replace(":", " ");

        sHTML = sHTML.Replace("*", " ");

        sHTML = sHTML.Replace("+", " ");

        sHTML = sHTML.Replace("=", " ");

        sHTML = sHTML.Replace("\"", " ");

        sHTML = sHTML.Replace("-", " ");

        sHTML = sHTML.Replace("_", " ");

        sHTML = sHTML.Replace("!", " ");

        sHTML = sHTML.Replace("?", " ");

        sHTML = sHTML.Replace("~", " ");

        sHTML = sHTML.Replace("#", " ");

        sHTML = sHTML.Replace("$", " ");

        sHTML = sHTML.Replace("%", " ");

        sHTML = sHTML.Replace("`", " ");

        sHTML = sHTML.Replace("´", " ");

        sHTML = sHTML.Replace("°", " ");

        sHTML = sHTML.Replace("^", " ");

 

        sHTML = sHTML.Replace("&", " ");

 

 

        while (sHTML.IndexOf("  ") >= 0)

        { sHTML = sHTML.Replace("  ", " "); }

 

        //< output >

        return sHTML;

        //</ output >

        //--------</ remove_Punctuation_Mark_Characters() >--------

    }

 

    public static string remove_Brackets_Characters(string sHTML)

    {

        //--------< remove_Brackets_Characters() >--------

        sHTML = sHTML.Replace("(", " ");

        sHTML = sHTML.Replace(")", " ");

        sHTML = sHTML.Replace("[", " ");

        sHTML = sHTML.Replace("]", " ");

        sHTML = sHTML.Replace("{", " ");

        sHTML = sHTML.Replace("}", " ");

 

        sHTML = sHTML.Replace("<", " ");

        sHTML = sHTML.Replace(">", " ");

 

 

 

        while (sHTML.IndexOf("  ") >= 0)

        { sHTML = sHTML.Replace("  ", " "); }

 

        //< output >

        return sHTML;

        //</ output >

        //--------</ remove_Brackets_Characters() >--------

    }

    public static string remove_Control_Characters(string sHTML)

    {

        //--------< HTML_to_Text() >--------

        sHTML = sHTML.Replace("\n", " ");

        sHTML = sHTML.Replace("\r", " ");

        sHTML = sHTML.Replace("\t", " ");

 

        while (sHTML.IndexOf("  ") >= 0)

        { sHTML = sHTML.Replace("  ", " "); }

 

        //< output >

        return sHTML;

        //</ output >

        //--------</ HTML_to_Text() >--------

    }

 

 

    public static string remove_Tags(string sHTML)

    {

        //--------< remove_Tags() >--------

 

        //----< @Loop: Search tags >----

        int intStart = -1;

        while (1 == 1)

        {

            //---< Search Tag >---

            //< check end >

            if (sHTML.Length <= intStart) break;

            //< check end >

 

            //< find open >

            int posOpenTag = sHTML.IndexOf("<", intStart + 1);

            if (posOpenTag < 0) break;

            //</ find open >

 

 

            //< find close >

            int posCloseTag = sHTML.IndexOf(">", posOpenTag);

            if (posCloseTag < 0) break; //no end tag

            //</ find close >

 

 

            //< cut Tag >

            string sLeft = sHTML.Substring(0, posOpenTag);

            string sRight = sHTML.Substring(posCloseTag + 1);

            sHTML = sLeft + " " + sRight;

            //</ cut Tag >

 

 

            intStart = sLeft.Length;

            //---</ Search Tag >---

        }

        //----</ @Loop: Search tags >----

       

 

        //< output >

        return sHTML;

        //</ output >

        //--------</ remove_Tags() >--------

    }

    public static string remove_script(string sHTML)

    {

        //--------< remove_script() >--------

 

        //----< @Loop: Search tags >----

        int intStart = 0;

        while (1 == 1)

        {

            //---< Search Tag >---

            //< check end >

            if (sHTML.Length <= intStart) break;

            //< check end >

 

            //< find open >

            int posscript_Open = sHTML.IndexOf("<script", intStart + 1,comparisonType:System.StringComparison.InvariantCultureIgnoreCase);

            if (posscript_Open < 0) break; //no open tag

            //</ find open >

 

            //< find close >

            int posscript_Close = sHTML.IndexOf("</script", posscript_Open + 1, comparisonType: System.StringComparison.InvariantCultureIgnoreCase);

            if (posscript_Close < 0) break; //no end tag

            //</ find close >

 

            //< find close >

            int posCloseTag = sHTML.IndexOf(">", posscript_Close);

            if (posCloseTag < 0) break; //no end tag

            //</ find close >

 

            //< cut Tag >

            string sLeft = sHTML.Substring(0, posscript_Open);

            string sRight = sHTML.Substring(posCloseTag+1 );

            sHTML = sLeft + sRight;

            //</ cut Tag >

 

 

            intStart = sLeft.Length;

            //---</ Search Tag >---

        }

        //----</ @Loop: Search tags >----

 

        //< output >

        return sHTML;

        //</ output >

        //--------</ remove_script() >--------

    }

    public static string remove_Head(string sHTML)

    {

        //--------< remove_Head() >--------

       

        //----< @Loop: Search tags >----

        int intStart = 0;

        while (1 == 1)

        {

            //---< Search Tag >---

            //< check end >

            if (sHTML.Length <= intStart) break;

            //< check end >

 

            //< find open >

            int posHead_Open = sHTML.IndexOf("<head", intStart + 1, comparisonType: System.StringComparison.InvariantCultureIgnoreCase);

            if (posHead_Open < 0) break; //no open tag

            //</ find open >

 

            //< find close >

            int posHead_Close = sHTML.IndexOf("</head", posHead_Open + 1, comparisonType: System.StringComparison.InvariantCultureIgnoreCase);

            if (posHead_Close < 0) break; //no end tag

            //</ find close >

 

            //< find close >

            int posCloseTag = sHTML.IndexOf(">", posHead_Close);

            if (posCloseTag < 0) break; //no end tag

            //</ find close >

 

            //< cut Tag >

            string sLeft = sHTML.Substring(0, posHead_Open);

            string sRight = sHTML.Substring(posCloseTag + 1);

            sHTML = sLeft + sRight;

            //</ cut Tag >

 

 

            intStart = sLeft.Length;

            //---</ Search Tag >---

        }

        //----</ @Loop: Search tags >----

 

        //< output >

        return sHTML;

        //</ output >

        //--------</ remove_Head() >--------

    }

 

 

}

 

 

Mobile
»
Asp.Net Core: Eigenen Email Provider einbinden
»
Chrome: wie kann man die Browser Informationen in Asp.Net Core 2 MVC ermitteln und anzeigen.
»
Client Auswertung mit Wangkanai Detection
»
Browser in Asp.Net Core 2 mit Wangkanai
»
Asp.Net Core 2 MVC: Email Sender Client schreiben.
»
Asp core: Cookie lesen und schreiben
»
Asp: Bild aktualisiert anzeigen trotz Browser Cache
»
Asp MVC: Formatiere Datum als Kurzes Datum
»
Asp MVC Core: HTML Text als HTML Elemente anzeigen
»
Asp: HTML in Text umwandeln

.

Jobs, Projekte, Angebote für Freiberufler, Selbstständig an Info@CodeDocu.de