Files
Customer-Panel/C4IT.API/ConverterHelper.cs
Drechsler, Meik 3a001d0e55 go
2025-08-14 16:20:42 +02:00

71 lines
2.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
namespace C4IT.API
{
public static class ConverterHelper
{
public static string Html2Plaintext(string html)
{
if (string.IsNullOrWhiteSpace(html))
return string.Empty;
// 1. HTMLEntities dekodieren (z.B. ü → ü)
string decoded = WebUtility.HtmlDecode(html);
// 2. </p> und <br> durch Zeilenumbruch ersetzen
string withBreaks = Regex.Replace(
decoded,
@"</(?:p|div|li|blockquote|pre|h[1-6]|tr)\s*> # schließende Block-Tags
|<(?:br|hr)\b[^>]*> # <br>, <hr>
|<(?:ul|ol|table|thead|tbody|tfoot|article # öffnende Block-Container
|section|nav|aside|header|footer
|figure|figcaption|details|summary)[^>]*>",
" ",
RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);
// 3. Alle übrigen Tags entfernen
string noTags = Regex.Replace(withBreaks, @"<[^>]+>", string.Empty);
return noTags.Trim();
}
public static bool ObjectToBoolConverter(object source)
{
try
{
if (source.Equals("1"))
{
source = 1;
}
return Convert.ToBoolean(source);
}
catch (Exception)
{
return false;
}
}
public static int ObjectToIntConverter(object source)
{
try
{
return Convert.ToInt32(source);
}
catch (Exception)
{
return 0;
}
}
public static string ObjectToStringConverter(this object value)
{
return (value ?? string.Empty).ToString();
}
}
}