removeHtmlTagsAndEntities function

String removeHtmlTagsAndEntities(
  1. String input
)

Removes all HTML tags and HTML entities from a given string.

This function uses a regular expression to replace:

  • HTML tags (e.g., <p>, <br>, <div>).
  • HTML entities (e.g., &nbsp;, &lt;, &amp;).

Example Usage:

String input = "<p>Hello &amp; Welcome</p>";
String result = removeHtmlTagsAndEntities(input);
logFile(key: result, name: 'remove html tags and entities'); // Output: "Hello  Welcome"

@param input The input string containing HTML tags or entities. @return A cleaned string with no HTML tags or entities.

Implementation

String removeHtmlTagsAndEntities(String input) {
  return input.replaceAll(RegExp(r'<[^>]*>|&[^;]+;'), ' ').trim();
}