开发者

How to change HTML codes to symbols iphone

开发者 https://www.devze.com 2023-03-01 09:57 出处:网络
I have written a function that changes texts like \"›\",开发者_如何转开发 \"«\" to symbols \"›\" \"«\" and I want to share this function with stackoverflow users. If

I have written a function that changes texts like "›",开发者_如何转开发 "«" to symbols "›" "«" and I want to share this function with stackoverflow users. If you have suggestions how to make this function better please write !!! Thank you !!!

- (NSString*) ChangeAccentsLettersToSymbols: (NSString*) strToCorrect {
    NSLog(@"ChangeAccentsLettersToSymbols Entered\n");
    static NSString * const codeMap[][2] = {
        {@"¡",    @"¡"},  {@"«",    @"«"},  {@"»",    @"»"},  {@"‹",   @"‹"},  
        {@"›",   @"›"},  {@"‚",    @"‚"},  {@"„",    @"„"},  {@"“",    @"“"},  
        {@"”",    @"”"},  {@"‘",    @"‘"},  {@"’",    @"’"},  {@"¢",     @"¢"},
        {@"£",    @"£"},  {@"¥",      @"¥"},  {@"€",     @"€"},  {@"¤",   @"¤"},  
        {@"&fnof;",     @"ƒ"},  {@"&gt;",       @">"},  {@"&lt;",       @"<"},  {@"&divide;",   @"÷"},  
        {@"&deg;",      @"°"},  {@"&not;",      @"¬"},  {@"&plusmn;",   @"±"},  {@"&micro;",    @"µ"},
        {@"&amp;",      @"&"},  {@"&reg;",      @"®"},  {@"&copy;",     @"©"},  {@"&trade;",    @"™"},  
        {@"&bull;",     @"•"},  {@"&middot;",   @"·"},  {@"&sect;",     @"§"},  {@"&ndash;",    @"–"},  
        {@"&mdash;",    @"—"},  {@"&dagger;",   @"†"},  {@"&Dagger;",   @"‡"},  {@"&loz;",      @"◊"},
        {@"&uarr;",     @"↑"},  {@"&darr;",     @"↓"},  {@"&larr;",     @"←"},  {@"&rarr;",     @"→"},  
        {@"&harr;",     @"↔"},  {@"&iquest;",   @"¿"},  {@"&nbsp;",     @" "},  {@"&quot;",     @"\""}
    };
    int count = sizeof(codeMap)/sizeof(codeMap[0]);
    for( int i=0; i<count; ++i ) {
        strToCorrect = [ strToCorrect stringByReplacingOccurrencesOfString: codeMap[i][0] 
                                                                withString: codeMap[i][1] ];
    }

    for( int i=33; i<126; ++i) {
        NSString* whotToReplace = [NSString stringWithFormat:@"&#%d;", i];
        NSString* replaceWith   = [NSString stringWithFormat:@"%c", (char*)i ];
        strToCorrect = [strToCorrect stringByReplacingOccurrencesOfString: whotToReplace
                                                               withString: replaceWith ];
    }
    return strToCorrect;
}


there is repo on github -

https://github.com/mwaterfall/MWFeedParser

look at this functions

- (NSString *)stringByDecodingHTMLEntities;
- (NSString *)stringByEncodingHTMLEntities;

its working fine for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号