【问题标题】:xcode. Converting strings with characters in HTML codes to Unicode strings代码。将带有 HTML 代码中的字符的字符串转换为 Unicode 字符串
【发布时间】:2011-06-23 08:22:46
【问题描述】:

这是我遇到的问题。 iPhone上的程序应该与服务器同步,服务器也与Web版本的服务同步。 Web 服务以 HTML 代码(amp;、quote;、№ 等)将字符串中的特殊字符发送到服务器。我需要显示这些数据,这就是为什么我需要将这些符号转换为 xcode 可以解码和绘制的东西。 正如我所发现的,以 Unicode 结尾的 HTML 代码是相同的,差异仅在于格式(例如 HTML 中的 № 是 Unicode 中的 \u8470)。我试过在字符串中更改这种格式并将其编码为 UTF8。结果,现在我有了一个函数:

+(NSString *) replaceHTMLCodes:(NSString *)text{
NSLog(@"replacing HTML codes");
if (text){
    NSLog(@"%@", text);
    NSString *tmpString=[NSString stringWithString:text];
    tmpString = [text copy];
    NSString *tmpText = @"";
    int locAmp = [tmpString rangeOfString:@"&#"].location;
    NSString * Code = @"";
    int locComa;
    while (locAmp!=NSNotFound) {
        tmpText = [tmpText stringByAppendingString:[tmpString substringToIndex:locAmp]];
        tmpString = [tmpString stringByReplacingCharactersInRange:NSMakeRange(0, locAmp) withString:@""];
        locComa = [tmpString rangeOfString:@";"].location;
        Code = [NSString stringWithString:[tmpString substringWithRange:NSMakeRange(0, locComa)]];
        Code = [Code stringByReplacingOccurrencesOfString:@"&#" withString:@"\\u"];
        NSLog(@"%@", Code);
        tmpString = [tmpString stringByReplacingCharactersInRange:NSMakeRange(0, locComa+1) withString:@""];
        tmpText = [tmpText stringByAppendingFormat:@"%C", Code];
        locAmp = [tmpString rangeOfString:@"&#"].location;
    }
    tmpText = [tmpText stringByAppendingString:tmpString];
    NSLog(@"%@", tmpText);
    return tmpText;
}
else
    return text;
}

但它不能正常工作 - 它显示随机 Unicode 符号,而不是我想要的。我曾尝试使用 NSUTF8StringEncoding,但也没有用。

任何想法如何解决这个问题?我对转换代码是否正确?

【问题讨论】:

  • 一大早要消化这个有点多,但原则上我会说通过 HTML 解析器运行数据,你设置它输出 UTF-8 编码的文本。

标签: html xcode unicode encoding


【解决方案1】:

谢谢你,戴夫。你的回答很有用。最后,这是我的日常。希望对某人有用。

+(NSString *) replaceHTMLCodes:(NSString *)text{
if (text){
    NSString *tmpString=[NSString stringWithString:text];
    tmpString = [text copy];
    NSString *tmpText = @"";
    int locAmp = [tmpString rangeOfString:@"&"].location;
    NSString * Code = @"";
    int locComa;
    while (locAmp!=NSNotFound && locAmp!=-1) {
        tmpText = [tmpText stringByAppendingString:[tmpString substringToIndex:locAmp]];
        tmpString = [tmpString stringByReplacingCharactersInRange:NSMakeRange(0, locAmp) withString:@""];
        locComa = [tmpString rangeOfString:@";"].location;
        Code = [NSString stringWithString:[tmpString substringWithRange:NSMakeRange(0, locComa)]];
        Code = [Code stringByReplacingOccurrencesOfString:@"&" withString:@""];
        if ([Code characterAtIndex:0]=='#') {
            Code = [Code stringByReplacingOccurrencesOfString:@"#" withString:@""];
            tmpText = [tmpText stringByAppendingFormat:@"%C", [Code intValue]];
        } else {
            if ([Code compare:@"amp"]==NSOrderedSame) {
                tmpText = [tmpText stringByAppendingString:@"&"];
            } else if ([Code compare:@"quot"]==NSOrderedSame) {
                tmpText = [tmpText stringByAppendingString:@"\""];   
            } else if ([Code compare:@"gt"]==NSOrderedSame) {
                tmpText = [tmpText stringByAppendingString:@">"];
            } else if ([Code compare:@"lt"]==NSOrderedSame) {
                tmpText = [tmpText stringByAppendingString:@"<"];
            } else if ([Code compare:@"laquo"]==NSOrderedSame) {
                tmpText = [tmpText stringByAppendingString:@"«"];
            } else if ([Code compare:@"raquo"]==NSOrderedSame) {
                tmpText = [tmpText stringByAppendingString:@"»"];
            }
        }
        tmpString = [tmpString stringByReplacingCharactersInRange:NSMakeRange(0, locComa+1) withString:@""];
        locAmp = [tmpString rangeOfString:@"&"].location;
    }
    tmpText = [tmpText  stringByAppendingString:tmpString];
    return tmpText;
}
else
    return text;
}

也许,这并不理想,但它对我有用。

【讨论】:

    【解决方案2】:

    您的日常工作中有一两个小错误。这个版本好像可以用...

    NSString * replaceHTMLCodes(NSString *text)
    {
    
        if (text){
            NSString *tmpString=[NSString stringWithString:text];
            tmpString = [text copy];
            NSString *tmpText = @"";
            int locAmp = [tmpString rangeOfString:@"&#"].location;
            NSString * Code = @"";
            int locComa;
            while (locAmp!=NSNotFound && locAmp != -1) {
                tmpText = [tmpText stringByAppendingString:[tmpString substringToIndex:locAmp]];
                tmpString = [tmpString stringByReplacingCharactersInRange:NSMakeRange(0, locAmp) withString:@""];
                locComa = [tmpString rangeOfString:@";"].location;
                Code = [NSString stringWithString:[tmpString substringWithRange:NSMakeRange(0, locComa)]];
                Code = [Code stringByReplacingOccurrencesOfString:@"&#" withString:@""];
    
                tmpString = [tmpString stringByReplacingCharactersInRange:NSMakeRange(0, locComa+1) withString:@""];
                tmpText = [tmpText stringByAppendingFormat:@"%C", [Code intValue]];
    
                locAmp = [tmpString rangeOfString:@"&#"].location;
            }
            tmpText = [tmpText stringByAppendingString:tmpString];
    
            return tmpText;
        }
        else
            return text;
    }
    

    【讨论】:

    • 感谢您的帮助!看来,我需要更仔细地学习手册!真的很管用。
    【解决方案3】:

    这是我的版本。可以找到可用代码列表here 我为 NSString 创建了类别:

    @interface NSString (HTMLDecode)
    
    - (NSString *)htmlfDecodedString;
    
    @end
    
    @implementation NSString (HTMLDecode)
    
    - (NSString *)htmlfDecodedString{
    
        NSDictionary *codesToSymbols = @{@"&quot;" : @"\"",
                                         @"&amp;"  : @"&",
                                         @"&lt;"   : @"<",
                                         @"&gt;"   : @">",
                                         @"&euro;" : @"€",
                                         @"&laquo;" : @"«",
                                         @"&raquo;" : @"»"};
    
        NSMutableString *str = [self mutableCopy];
    
        [codesToSymbols enumerateKeysAndObjectsUsingBlock:^(NSString  *key, NSString  *value, BOOL *stop) {
            [str replaceOccurrencesOfString:key withString:value options:NSCaseInsensitiveSearch range:NSMakeRange(0, str.length)];
        }];
    
        return str;
    }
    
    @end
    

    怎么用?

    就这么简单:

    NSString *html =
    @"<table>\
        <tbody>\
            <tr>\
                <td>Testing html symbols. Ampersand:&amp;. &laquo;Hello Double&raquo;. &lsquo;Hello single!&lsquo;\
                </td>\
            </tr>\
        </tbody>\
    </table>";
    
    
    NSString *result = [html htmlfDecodedString];
    
    NSLog(@"converted html:\n%@",result);
    

    它会产生这样的html:

    <table><tbody><tr><td>Testing html symbols. Ampersand:&. «Hello Double». 'Hello single!'</td></tr></tbody></table>
    

    【讨论】:

      猜你喜欢
      • 2021-07-10
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 2020-01-21
      • 2014-09-30
      • 2018-11-13
      • 2019-06-13
      相关资源
      最近更新 更多