How can I reliably convert special characters like E equivalents to HTML using Cocoa?

I am trying to pass some simple html to Mail.app: lists, bold, italics. However, I noticed that if I use characters like £ , then Mail.app just doesn't show anything. I realized that I needed to convert to HTML objects like £ (full list here: http://www.w3schools.com/tags/ref_entities.asp ). I have a partial solution that works for most of the characters that my users have come up with, but this is far from a solid fix:

 - (NSString*) makeValidHTML:(NSString*)str { str = [str stringByReplacingOccurrencesOfString:@"£" withString:@"&pound;"]; str = [str stringByReplacingOccurrencesOfString:@"¢" withString:@"&cent;"]; str = [str stringByReplacingOccurrencesOfString:@"¥" withString:@"&yen;"]; str = [str stringByReplacingOccurrencesOfString:@"©" withString:@"&copy;"]; str = [str stringByReplacingOccurrencesOfString:@"®" withString:@"&reg;"]; str = [str stringByReplacingOccurrencesOfString:@"°" withString:@"&deg;"]; str = [str stringByReplacingOccurrencesOfString:@"¿" withString:@"&iquest;"]; str = [str stringByReplacingOccurrencesOfString:@"¡" withString:@"&iexcl;"]; str = [str stringByReplacingOccurrencesOfString:@"'" withString:@"'"]; str = [str stringByReplacingOccurrencesOfString:@"'" withString:@"'"]; str = [str stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"]; str = [str stringByReplacingOccurrencesOfString:@"\"" withString:@"&quot;"]; str = [str stringByReplacingOccurrencesOfString:@""" withString:@"&quot;"]; str = [str stringByReplacingOccurrencesOfString:@"<" withString:@"&lt;"]; str = [str stringByReplacingOccurrencesOfString:@">" withString:@"&gt;"]; return str; } 

Is there a standard way to do this without having to list all possible reserved characters?

+4
source share
2 answers

This class should be useful for you:
https://github.com/mwaterfall/MWFeedParser/blob/master/Classes/NSString+HTML.m

Link derived from this other SO answer:
Convert & to Objective-C

+4
source

I think your main problem is that you are not coding or declaring your HTML page as UTF-8. Although some of the objects you mentioned are a genuine problem and need to be converted, for example > to &gt; (the @Joel Martinez code associated with this will help there) things like the £ character will work just fine as they are if the page is declared and encoded as Unicode format like UTF-8:

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

0
source

Source: https://habr.com/ru/post/1369332/


All Articles