gpt4 book ai didi

ios - iOS中HTML标签的字符串替换

转载 作者:行者123 更新时间:2023-12-01 18:59:11 24 4
gpt4 key购买 nike

我正在解析 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 &amp; Reviews</a>.</p>

我想要的是 删除文本的超链接。我不想删除文本我只想禁用文本的超链接 .

因此,对于上述数据,它应该看起来像 -
 <p>The post Triumph Motorcycles Plans To Sell Over 2700 Units By 2015-16 appeared first on MotorBuzz -  Car Bike News &amp; 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 &amp; 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com