I’ve been trying to achieve this for a while, I have a string which contains a lot of HTML tags in it which is in some encoded form Like & lt; and & gt; (without the spaces) in between the string. Can anyone assist me in removing those tags so that I can get a plain string?
Finally I achieved this using the html package
Here’s how I did it
import 'package:html/parser.dart';
//here goes the function
String _parseHtmlString(String htmlString) {
final document = parse(htmlString);
final String parsedString = parse(document.body.text).documentElement.text;
return parsedString;
}
I don’t know if there is any cleaner way to do this but this one worked for me.
You can simply use RegExp without 3rd Lib for remove tag (
</>)
String removeAllHtmlTags(String htmlText) {
RegExp exp = RegExp(
r"<[^>]*>",
multiLine: true,
caseSensitive: true
);
return htmlText.replaceAll(exp, '');
}
'<a title="1 < 3, but 3 > 2">Don't use regex to parse HTML</a>'
, '2">Don't use regex to parse HTML'
will be left after replacing instead of 'Don't use regex to parse HTML'
–
Hyperopia The intl
package provides a method stripHtmlIfNeeded
to strip the HTML tags from the string.
The Bidi
class under this package provides the utility method for working with the bidirectional text.
import 'package:intl/intl.dart';
Bidi.stripHtmlIfNeeded("<p>Hello World</p>")
If you don't want to use the whole package just for this function, below is the method implementation:
static String stripHtmlIfNeeded(String text) {
return text.replaceAll(RegExp(r'<[^>]*>|&[^;]+;'), ' ');
}
Documentation: https://api.flutter.dev/flutter/intl/Bidi/stripHtmlIfNeeded.html
By just using
import ‘package:html/parser.dart’;
will get a problem, for those strings that includes <br>
and <p>
tags. Paragraph info is missing. May first replace <br>
to <p>
, then get List:
import ‘package:html/parser.dart’ as dom;
htmlString = '<p> first ... line.<br>second.....line.<p>';
List<String> cleanStrings = new List<String>();
List<dom.Element> ps = parse(htmlString.replaceAll('<br>', '</p><p>'))).querySelectorAll('p');
if (ps.isNotEmpty) ps.forEach((f) {
(f.text != '') cleanStrings.add(f.text);
});
Here is my solution if using flutter web or can't import the parser for any reason and it's configurable.
String formatHtmlString(String string) {
return string
.replaceAll("\n\n", "<p>") // Paragraphs
.replaceAll("\n", "<br>") // Line Breaks
.replaceAll("\"", """) // Quote Marks
.replaceAll("'", "'") // Apostrophe
.replaceAll(">", "<") // Less-than Comparator (Strip Tags)
.replaceAll("<", ">") // Greater-than Comparator (Strip Tags)
.trim(); // Whitespace
}
If you want to Decode the HTML content to String then Follow this step:
- Add this plugin to
pubspec.yaml
=> HTML Parser - Dart Library
Then in your code add this Line =>
String htmlText = parse("String with HTML tags").body!.text
3 steps
first, add this to your "pubspec.yaml" file
dependencies: flutter_html: ^0.8.2
second, import to your dart file
import 'package:flutter_html_view/flutter_html_view.dart';
3rd, simply use
HtmlView(data: "Your Html Data"),
© 2022 - 2024 — McMap. All rights reserved.
html
package supports modifying (never used it). Perhaps you can answer your question with some example code? – Rufe