Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to strip HTML from a QString in Qt?

Tags:

html

strip

qt

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.

like image 455
Nathan Osman Avatar asked May 09 '10 22:05

Nathan Osman


People also ask

How do I strip a tag in HTML?

The strip_tags() function strips a string from HTML, XML, and PHP tags. Note: HTML comments are always stripped. This cannot be changed with the allow parameter. Note: This function is binary-safe.

How do I strip a string in HTML?

To strip out all the HTML tags from a string there are lots of procedures in JavaScript. In order to strip out tags we can use replace() function and can also use . textContent property, . innerText property from HTML DOM.

How do I remove all tags from a string?

The HTML tags can be removed from a given string by using replaceAll() method of String class.

What does it mean to strip HTML?

stripHtml( html ) Changes the provided HTML string into a plain text string by converting <br> , <p> , and <div> to line breaks, stripping all other tags, and converting escaped characters into their display values.


2 Answers

QString s = "<i>Test:</i><img src=\"blah.png\" /><br> A test case"; s.remove(QRegExp("<[^>]*>")); // s == "Test: A test case" 
like image 116
k06a Avatar answered Sep 19 '22 17:09

k06a


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.

like image 21
Vishesh Handa Avatar answered Sep 17 '22 17:09

Vishesh Handa