30

I have a QString with some HTML in it... is there an easy way to strip the HTML from it? I basically want just the actual text content.

<i>Test:</i><img src="blah.png" /><br> A test case

Would become:

Test: A test case

I'm curious to know if Qt has a string function or utility for this.

Nathan Osman
  • 71,149
  • 71
  • 256
  • 361

5 Answers5

38
QString s = "<i>Test:</i><img src=\"blah.png\" /><br> A test case";
s.remove(QRegExp("<[^>]*>"));
// s == "Test: A test case"
k06a
  • 17,755
  • 10
  • 70
  • 110
32

If you don't care about performance that much then QTextDocument does a pretty good job of converting HTML to plain text.

QTextDocument doc;
doc.setHtml( htmlString );

return doc.toPlainText();

I know this question is old, but I was looking for a quick and dirty way to handle incorrect HTML. The XML parser wasn't giving good results.

Vishesh Handa
  • 1,830
  • 2
  • 14
  • 17
14

You may try to iterate through the string using QXmlStreamReader class and extract all text (if you HTML string is guarantied to be well formed XML).

Something like this:

QXmlStreamReader xml(htmlString);
QString textString;
while (!xml.atEnd()) {
    if ( xml.readNext() == QXmlStreamReader::Characters ) {
        textString += xml.text();
    }
}

but I'm unsure that its 100% valid ussage of QXmlStreamReader API since I've used it quite longe time ago and may forget something.

VestniK
  • 1,910
  • 2
  • 17
  • 29
1

the situation that some html is not quite validate xml make it worse to work it out correctly.

If it's valid xml (or not too bad formated), I think QXmlStreamReader + QXmlStreamEntityResolver might not be bad idea.

Sample code in: https://github.com/ycheng/misccode/blob/master/qt_html_parse/utils.cpp

(this can be a comment, but I still don't have permission to do so)

-5

this answer is for who read this post later and using Qt5 or later. simply escape the html characters using inbuilt functions as below.

QString str="<h1>some hedding </h1>"; // a string containing html tags.
QString esc=str.toHtmlEscaped(); //esc contains the html escaped srring.
danial weaber
  • 846
  • 2
  • 17
  • 37