Re: escape special characters in HTML
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. "ê" as this can be done generically
in a loop without having to know that ~ is ∼ 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:@"&"
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