2013-03-11 103 views
1

我是一个noob iphone开发,我试图将html数据转换为字符串格式。目前,我正在使用stringByConvertingHTMLToPlainText来完成此操作,但它忽略了HTML的格式。我要转换的HTML数据有换行符,但stringByConvertingHTMLToPlainText只删除HtML标签并且不格式化结果字符串。任何帮助是极大的赞赏。如何正确地将HTML转换为字符串?

我的代码:

NSString *temp = [[[stories objectAtIndex: storyIndex] objectForKey: @"summary"]stringByConvertingHTMLToPlainText]; 

HTML数据:

Holiday Inn<br/>Dedham,MA<br/>Sun May 5th 2013-Sun May 5th 2013<br/>Contact: Harry Tong at 603-978-3459<p><sub><i>-- Delivered by <a href="http://feed43.com/">Feed43</a> service</i></sub></p> 

得到的字符串:

Holiday InnDedham,MASun May 5th 2013-Sun May 5th 2013Contact: Harry Tong at 603-978-3459-- Delivered by "http://feed43.com/"Feed43 service 

我需要:

Holiday Inn 
Dedham,MA 
Sun May 5th 2013-Sun May 5th 2013 
Contact: Harry Tong at 603-978-3459 
-- Delivered by "http://feed43.com/"Feed43 service 

编辑

NSString *html = [[stories objectAtIndex: storyIndex] objectForKey: @"summary"]; 
NSString *text = [html stringByReplacingOccurrencesOfString:@"<br/>" withString:@" \n "]; 
NSString *temp = [text stringByConvertingHTMLToPlainText]; 

回答

1

我想你应该只是换行符替换< BR/>(也许<BR>老年HTML的情况下)。在您正在编程的语言中Google换行符,并使用字符串替换函数。

+0

感谢您的回复。你的回答很有道理,但由于某种原因,它对我不起作用。请检查我的编辑 – 2013-03-11 22:35:01

+0

在*文本中跳过第三步(* temp = ...)您已经拥有了您想要的内容 – Prozi 2013-03-11 23:01:17

1

@Prozi是对的。您可以使用

NSString *text = [html stringByReplacingOccurrencesOfString:@"<br/>" withString:@"\n"]; 

做那您删除HTML标签。

+0

感谢您的回复。你的回答很有道理,但由于某种原因,它对我不起作用。请检查我的编辑。 – 2013-03-11 22:31:50