Convert formatted HTML text string to NSString parts

chatur picture chatur · Nov 16, 2011 · Viewed 19.8k times · Source

I want to decode HTML string and store it in NSString.

following is the html string for one of the response from google direction api.

teststring is Turn <b>right</b> onto <b>Kennington Park Rd/A3</b><div style="font-size:0.9em">Continue to follow A3</div><div style="font-size:0.9em">Entering toll zone in 1.7&nbsp;km at Newington Causeway/A3</div><div style="font-size:0.9em">Go through 2 roundabouts</div>

I want to store this html string in Array of 4 different NSStrings with following 4 NSStrings (removing all information for size colour)

Turn right onto Kennington Park Rd/A3
Continue to follow A3
Entering toll zone in 1.7 km at Newington Causeway/A3
Go through 2 roundabouts

I have used following method to convert html string to plain text.(html_response is the response from server and stringByConvertingHTMLToPlainText is the method defined in custom class file)

testString = (NSString*) [html_response stringByConvertingHTMLToPlainText];
NSLog(@"%@",testString);

but it converts whole string instead of breaking it in parts. output at the console is

Turn right onto Kennington Park Rd/A3Continue to follow A3 Entering toll zone in 1.7 km at Newington Causeway/A3 Go through 2 roundabouts

So do I need to write custom method to do this? Any help will be greatly appreciated.

Answer

Goles picture Goles · Nov 16, 2011

There's a very nice NSString category that you can use, it's a part of the project MWFeedParser. More specifically, you look for the file NSString+HTML.

The NSString+HTML category adds the following methods to the NSString Class,

- (NSString *)stringByStrippingTags;
- (NSString *)stringWithNewLinesAsBRs;
- (NSString *)stringByRemovingNewLinesAndWhitespace;
- (NSString *)stringByDecodingHTMLEntities;
- (NSString *)stringByEncodingHTMLEntities;

You could then try something like:

NSString *summary = [[[htmlString stringByStrippingTags] stringByRemovingNewLinesAndWhitespace] stringByDecodingHTMLEntities];

Hope it helps!, good luck :)