我正在解析 UIWebview
的字符串在 iOS
.
我有一个数据 -
<p>The post <a rel="nofollow" href="http://www.someurl.com/bikes/triumph-motorcycles-plans-sell-2700-units-2015-16/">Triumph Motorcycles Plans To Sell Over 2700 Units By 2015-16</a> appeared first on <a rel="nofollow" href="http://www.someurl.com">MotorBuzz - Car Bike News & Reviews</a>.</p>
我想要的是删除文本的超链接。我不想删除文本我只想禁用文本的超链接 .
因此,对于上述数据,它应该看起来像 -
<p>The post Triumph Motorcycles Plans To Sell Over 2700 Units By 2015-16 appeared first on MotorBuzz - Car Bike News & Reviews.</p>
所以请让我知道 中的任何正则表达式或任何字符串替换解决方案iOS/目标 -C .
谢谢。
最佳答案
您可以使用NSAttributedString
.首先,您将 html 字符串转换为 NSAttributedString
,然后从中获取实际的字符串。
NSString *html = @"<p>The post <a rel=\"nofollow\" href=\"http://www.someurl.com/bikes/triumph-motorcycles-plans-sell-2700-units-2015-16/\">Triumph Motorcycles Plans To Sell Over 2700 Units By 2015-16</a> appeared first on <a rel=\"nofollow\" href=\"http://www.someurl.com\">MotorBuzz - Car Bike News & Reviews</a>.</p>";
NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
documentAttributes:nil error:nil];
NSString *actualString= [attrString string];
但它会从字符串中删除所有 html 标签。
关于ios - iOS中HTML标签的字符串替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24057278/