2011-06-23 21 views
0

ここに私の問題があります。 iPhoneのプログラムはサーバーと同期する必要があります。サーバーはウェブバージョンのサービスと同期します。 Webサービスは文字列の特殊文字をHTMLコードでサーバーに送信します(& #amp; &#quote; №など)。私はこのデータを表示する必要があります。なぜなら、このシンボルを何かに変換する必要があり、xcodeがデコードして描画する必要があるからです。 私が発見したように、UnicodeでのHTMLの最後のコードは同じですが、違いはフォーマットのみです(HTMLの№はUnicodeの\ u8470です)。私は文字列でこの形式を変更し、UTF8としてエンコードしてみました。その結果、今私が機能を持っている:xcode。 HTMLコードの文字列をUnicode文字列に変換する

+(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を使用しようとしましたが、それも役に立たなかった。

これを解決する方法はありますか?私はコードを変換して正しいですか?

+0

:それはこのようなHTMLが生成されます

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); 

UTF-8エンコードされたテキストを出力するように設定したHTMLパーサーによるデータ。 –

答えて

0

あなたのルーチンには小さなバグが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; 
} 
+0

ありがとうございます!私はマニュアルをより慎重に学習する必要があるようだ!それは実際に動作します。 – svilon

2

ありがとう、Dave。あなたの答えは役に立ちました。最後に、ここに私のルーチンです。希望、それは誰かにとって有益だろう。

+(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; 
} 

多分、それは理想的ではありませんが、それは私のために働きます。

0

これは私のバージョンです。それを使用する方法

@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のためのカテゴリを作成しhere を見つけることができますか?

そのような単純な:これは、午前中に、これは早く消化するビットくらいですが、原則的に、私は実行を言うと思います

<table><tbody><tr><td>Testing html symbols. Ampersand:&. «Hello Double». 'Hello single!'</td></tr></tbody></table> 
関連する問題