简体   繁体   English

如何将html文本解析为无属性字符串的纯文本?

[英]How to parse html text into plain text without attributed string?

How do I parse html text into plain text without attributed string? 如何将html文本解析为纯文本而没有属性字符串?

This is my code: 这是我的代码:

(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 whitespaceCharacterSet]];
    return html;
}

假设您可以访问某种UIWEbView ,则可以执行一些JavaScript来检索包含要字符串化的HTML的元素的.text()

You can use the below code and get from here 您可以使用以下代码并从此处获取

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

NSString *hstmString = @"This is <font color='red'>simple</font>";

NSString* strWithoutFormatting = [self stringByStrippingHTML:hstmString];

NSLog(@"%@", strWithoutFormatting);

It maybe help you :) 它可能对您有帮助:)

If using a library is an option you could try HTMLKit . 如果可以选择使用库,则可以尝试HTMLKit

For example, given the following HTML: 例如,给定以下HTML:

<p>Some <b>text</b> to <em>extract</em></p>

one way to parse it to plain text would be: 将其解析为纯文本的一种方法是:

// create a <div> element
HTMLElement *element = [[HTMLElement alloc] initWithTagName:@"div"];
// set its innerHTML
element.innerHTML = @"<p>Some <b>text</b> to <em>extract</em></p>";
// textContext of the element contains all the text
NSLog(@"%@", element.textContent);
// You get: 'Some text to extract'

Let me know if you need further help. 让我知道您是否需要进一步的帮助。

If your HTML is simple and parsing it is not the core functionality of your app/project, then maybe HTMLKit is not for you, since it is a full-fledged HTML parser. 如果您的HTML很简单,并且解析不是您的应用程序/项目的核心功能,那么HTMLKit可能不适合您,因为它是成熟的HTML解析器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS html 文本在属性字符串中无法正常工作 - iOS html text is not working properly in attributed string 如何获得HTML归属文本的真实高度 - How to get real height of HTML attributed text 将包含Latex Comand for Mathematical formula的HTML文本转换为输出正确公式的普通或属性文本 - Converting HTML text that contains Latex Comand for Mathematical formula to plain or attributed text outputting correct formula 带有文本附件和截断的属性字符串 - Attributed string with text attachment and truncation 如何制作包含属性文本但仅复制纯文本的 NSTextField - How can I make an NSTextField containing attributed text but only copy plain text 如何使用核心文本和属性字符串伪造上标和下标? - How can I fake superscript and subscript with Core Text and an Attributed String? 如何更改某些属性字符串的文本颜色或使其变为粗体? - How to change a text color for a certain attributed string or make it bold? 根据其原始纯文本状态创建UIButton属性标题 - Creating UIButton attributed title based on its original plain text state 如何在iPhone中将HTML标签转换为纯文本 - How to convert Html tags to plain text in iPhone 如何使用NSTextContainer和NSTextStorage在UITextView中显示属性文本 - How to display attributed text in a UITextView with NSTextContainer and NSTextStorage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM