Re: String->HTML
Re: String->HTML
- Subject: Re: String->HTML
- From: Jeff LaMarche <email@hidden>
- Date: Wed, 28 Sep 2005 00:53:10 -0400
On Sep 27, 2005, at 6:18 PM, Scott Ribe wrote:
Is there any method anywhere to take an arbitrary string and get it
back as
HTML-encoded entities. *Not* URL encoding, but & to & > to >
and so
on. I can write this, but it just seems like something that ought
be there
somewhere. (Strings are 99.99% guaranteed to be plain ASCII;
possibility of
high-ASCII MacRoman encoding is so rare that I can ignore it; no other
encodings need be supported.)
Here's a method that Clark S. Cox posted to the list a few years ago
and I've kept in my snippets folder... I think it's what you're
looking for:
NSString *EncodeForHTML(NSString *input)
{
unsigned length = [input length];
unichar *buffer = malloc(length * sizeof *buffer);
if(buffer)
{
NSMutableString *result = [NSMutableString string];
for(unsigned i=0; i<length; ++i)
{
if(buffer[i] >= 'a' && buffer[i] <= 'z' ||
buffer[i] >= 'A' && buffer[i] <= 'Z' ||
buffer[i] >= '0' && buffer[i] <= '9')
{
[result appendFormat: @"%c", buffer[i]];
}
else
{
[result appendFormat: @"&#x%x;", buffer[i]];
}
}
free(buffer);
return result;
}
return nil;
}
_______________________________________________
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