Skip to content

Commit 13ece0e

Browse files
committed
Enhance StripHtml method in StringExtensions.cs
Updated the `StripHtml` method to replace closing paragraph tags and `<br>` tags with a space, while preserving the functionality of removing all other HTML tags.
1 parent b133d41 commit 13ece0e

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

source/DasBlog.Web.Core/Extensions/StringExtensions.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@ public static string RemoveLineBreaks(this string text)
1515
return text;
1616
}
1717

18-
public static string StripHtml(this string text)
19-
{
20-
text = Regex.Replace(text, "<.*?>", string.Empty, RegexOptions.Compiled);
21-
text = text.Replace("<", "");
22-
text = text.Replace(">", "");
23-
text = text.Replace("&quot;", "");
24-
return text;
25-
}
18+
public static string StripHtml(this string text)
19+
{
20+
// Replace closing paragraph tags and <br> tags with a space, allowing for optional whitespace
21+
text = Regex.Replace(text, "</\\s*p\\s*>|<\\s*br\\s*/?>", " ", RegexOptions.IgnoreCase | RegexOptions.Compiled);
22+
23+
// Remove all other HTML tags
24+
text = Regex.Replace(text, "<.*?>", string.Empty, RegexOptions.Compiled);
25+
26+
text = text.Replace("<", "");
27+
text = text.Replace(">", "");
28+
text = text.Replace("&quot;", "");
29+
30+
return text;
31+
}
2632

2733
public static string StripHTMLFromText(this string text)
2834
{

0 commit comments

Comments
 (0)