gpt4 book ai didi

objective-c - 仅从 Objective-C 中的 html 内容中提取文本

转载 作者:行者123 更新时间:2023-11-28 20:38:14 26 4
gpt4 key购买 nike

我发现的所有 strip 函数都是从 html 内容中提取 html 元素。我正在寻找一个简单的 objective-c 函数,它给出了一个嵌套的文本 block ,例如:

<table border="0" cellpadding="2" cellspacing="7" style="vertical-align:top;"><tr><td width="80" align="center" valign="top"><font style="font-size:85%;font-family:arial,sans-serif"></font></td><td valign="top" class="j"><font style="font-size:85%;font-family:arial,sans-serif"><br /><div style="padding-top:0.8em;"><img alt="" height="1" width="1" /></div><div class="lh"><a href="http://news.google.com/news/url?sa=t&amp;fd=R&amp;usg=AFQjCNFV5azq03nECHSmTV0CI-KwzBFXWA&amp;url=http://www.fool.com/investing/general/2012/03/11/the-justice-department-has-apples-number.aspx"><b>The Justice Department Has ordered <b>Apple</b> .... 

它只会返回 The justice Department has ordered apple ....

我知道有一个 UIWebView Javascript 函数可以执行此操作,但它似乎有点慢,因为它依赖于 javascript。我想知道是否有给定带有嵌套标签的 html 的函数(它将忽略所有标签及其内容并返回纯内容文本)

谢谢,罗斯

最佳答案

只需使用尖括号拆分字符串,取出所有其他元素,然后将它们重新连接在一起:

NSArray *components = [yourString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];

NSMutableArray *componentsToKeep = [NSMutableArray array];
for (int i = 0; i < [components count]; i = i + 2) {
[componentsToKeep addObject:[components objectAtIndex:i]];
}

NSString *plainText = [componentsToKeep componentsJoinedByString:@""];

关于objective-c - 仅从 Objective-C 中的 html 内容中提取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9659868/

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