使用Objective-C将HTML文本转换为纯文本

时间:2013-10-07 13:49:12

标签: html ios objective-c nsstring

我内置了大量NSString HTML文本。该字符串的长度超过3.500.000个字符。如何将此HTML文本转换为NSString内部的纯文本。我使用的是扫描仪,但效果太慢了。有什么想法吗?

8 个答案:

答案 0 :(得分:67)

这取决于您定位的iOS版本。从iOS7开始,有一种内置方法,不仅可以去除HTML标记,还可以将格式设置为字符串:

Xcode 9 / Swift 4

if let htmlStringData = htmlString.data(using: .utf8), let attributedString = try? NSAttributedString(data: htmlStringData, options: [.documentType : NSAttributedString.DocumentType.html], documentAttributes: nil) {
    print(attributedString)
}

您甚至可以创建如下的扩展程序:

extension String {
    var htmlToAttributedString: NSAttributedString? {
        guard let data = self.data(using: .utf8) else {
            return nil
        }

        do {
            return try NSAttributedString(data: data, options: [.documentType : NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
        } catch {
            print("Cannot convert html string to attributed string: \(error)")
            return nil
        }
    }
}

请注意,此示例代码使用的是UTF8编码。您甚至可以创建函数而不是计算属性,并将编码添加为参数。

Swift 3

let attributedString = try NSAttributedString(data: htmlString.dataUsingEncoding(NSUTF8StringEncoding)!,
                                              options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                                              documentAttributes: nil)

<强>目标C

[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];

如果您只需要删除<>之间的所有内容(脏话!!!),如果您在字符串中包含这些字符可能会出现问题,请使用以下命令:

- (NSString *)stringByStrippingHTML {
   NSRange r;
   NSString *s = [[self copy] autorelease];
   while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
     s = [s stringByReplacingCharactersInRange:r withString:@""];
   return s;
}

答案 1 :(得分:16)

我用扫描仪解决了我的问题,但我不是用它来处理所有文本。在将所有部分连接在一起之前,我将它用于每10,000个文本部分。我的代码在下面

-(NSString *)convertHTML:(NSString *)html {

    NSScanner *myScanner;
    NSString *text = nil;
    myScanner = [NSScanner scannerWithString:html];

    while ([myScanner isAtEnd] == NO) {

        [myScanner scanUpToString:@"<" intoString:NULL] ;

        [myScanner scanUpToString:@">" intoString:&text] ;

        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text] withString:@""];
    }
    //
    html = [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    return html;
}

Swift 4:

var htmlToString(html:String) -> String {
        var htmlStr =html;
        let scanner:Scanner = Scanner(string: htmlStr);
        var text:NSString? = nil;
        while scanner.isAtEnd == false {
            scanner.scanUpTo("<", into: nil);
            scanner.scanUpTo(">", into: &text);
            htmlStr = htmlStr.replacingOccurrences(of: "\(text ?? "")>", with: "");
        }
        htmlStr = htmlStr.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines);
        return htmlStr;
}

答案 2 :(得分:1)

对于Swift语言,

NSAttributedString(data:(htmlString as! String).dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true
            )!, options:[NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSNumber(unsignedLong: NSUTF8StringEncoding)], documentAttributes: nil, error: nil)!

答案 3 :(得分:1)

- (NSString *)stringByStrippingHTML:(NSString *)inputString
{
    NSMutableString *outString;

    if (inputString)
    {
        outString = [[NSMutableString alloc] initWithString:inputString];

        if ([inputString length] > 0)
        {
            NSRange r;

            while ((r = [outString rangeOfString:@"<[^>]+>|&nbsp;" options:NSRegularExpressionSearch]).location != NSNotFound)
            {
                [outString deleteCharactersInRange:r];
            }      
        }
    }

    return outString; 
}

答案 4 :(得分:1)

目标C

+ (NSString*)textToHtml:(NSString*)htmlString
{
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&apos;" withString:@"'"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    return htmlString;
}

希望这有帮助!

答案 5 :(得分:0)

您是否尝试过类似下面的内容,不确定它是否会像您在使用扫描仪之前那样更快检查: -

//String which contains html tags
    NSString *htmlString=[NSString stringWithFormat:@"%@",@"<b>right</b> onto <b>Kennington Park Rd/A3</b>Continue to follow A3</div><div >Entering toll zone in 1.7&nbsp;km at Newington Causeway/A3</div><divGo through 2 roundabouts</div>"];


    NSMutableString *mutStr=[NSMutableString string];
    NSString *s = nil;
//Removing html elements tags
    NSArray *arra=[htmlString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"</>"]];
    NSLog(@"%@",arra);
    for (s in arra)
    {
        [mutStr appendString:@" "];
        [mutStr appendString:s];
    }
            NSLog(@"%@",mutStr);//Printing the output

答案 6 :(得分:0)

斯威夫特4:

do {
   let cleanString = try NSAttributedString(data: htmlContent.data(using: String.Encoding.utf8)!,
                                                                      options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                                                                      documentAttributes: nil)
} catch {
    print("Something went wrong")
}

答案 7 :(得分:0)

通过传递编码类型作为参数可以更通用,但例如作为该类别:

@implementation NSString (CSExtension)

    - (NSString *)htmlToText {
        return [NSAttributedString.alloc
                initWithData:[self dataUsingEncoding:NSUnicodeStringEncoding]
                     options:@{NSDocumentTypeDocumentOption: NSHTMLTextDocumentType}
          documentAttributes:nil error:nil].string;
    }

@end