• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: escape special characters in HTML
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: escape special characters in HTML


  • Subject: Re: escape special characters in HTML
  • From: Conor <email@hidden>
  • Date: Thu, 16 Nov 2006 15:15:34 +0100

>Surely there is an easy way of doing that?

There exists a function to escape to percent escapes, but this is not what
you want. I would recommend writing your own function and escaping them to
numerals instead of the word; i.e. "&#234;" as this can be done generically
in a loop without having to know that ~ is &Tilde; Here is my class method
that does just that:

+ (NSMutableString *)encodeHTML:(id)aString {
    NSMutableString *stringToEncode = [NSMutableString string];
    [stringToEncode setString:aString];

    unsigned int i, count = [stringToEncode length];
    for (i =0; i < count; i++) {

        if ([stringToEncode characterAtIndex:i] > 127){
            NSString *replaceString = [NSString stringWithFormat:@"&#%d;",
[stringToEncode characterAtIndex:i]];
            [stringToEncode replaceCharactersInRange:NSMakeRange(i,1)
withString:replaceString];
            count = [stringToEncode length];
            i += [replaceString length] -1;
        }
    }

    return stringToEncode;
}


You might want to add the following line to encode the amp as well at the
beginning, depending on your needs:

[stringToEncode replaceOccurrencesOfString:@"&" withString:@"&amp;"
options:NSLiteralSearch range:NSMakeRange(0, [stringToEncode length])];


Regards,
Conor
http://www.bruji.com/


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: escape special characters in HTML
      • From: Dominik Pich <email@hidden>
  • Prev by Date: [SOLVED] Re: escape special characters in HTML
  • Next by Date: Re: escape special characters in HTML
  • Previous by thread: Re: [SOLVED] Re: escape special characters in HTML
  • Next by thread: Re: escape special characters in HTML
  • Index(es):
    • Date
    • Thread