ios html string,iOS HTML Unicode to NSString?

It's pretty easy to write your own HTML entity decoder. Just scan the string looking for &, read up to the following ;, then interpret the results. If it's "amp", "lt", "gt", or "quot", replace it with the relevant character. If it starts with #, it's a numeric entity. If the # is followed by an "x", treat the rest as hexadecimal, otherwise as decimal. Read the number, and then insert the character into your string (if you're writing to an NSMutableString you can use [str appendFormat:@"%C", thechar]. NSScanner can make the string scanning pretty easy, especially since it already knows how to read hex numbers.

I just whipped up a function that should do this for you. Note, I haven't actually tested this, so you should run it through its paces:

- (NSString *)stringByDecodingHTMLEntitiesInString:(NSString *)input {

NSMutableString *results = [NSMutableString string];

NSScanner *scanner = [NSScanner scannerWithString:input];

[scanner setCharactersToBeSkipped:nil];

while (![scanner isAtEnd]) {

NSString *temp;

if ([scanner scanUpToString:@"&" intoString:&temp]) {

[results appendString:temp];

}

if ([scanner scanString:@"&" intoString:NULL]) {

BOOL valid = YES;

unsigned c = 0;

NSUInteger savedLocation = [scanner scanLocation];

if ([scanner scanString:@"#" intoString:NULL]) {

// it's a numeric entity

if ([scanner scanString:@"x" intoString:NULL]) {

// hexadecimal

unsigned int value;

if ([scanner scanHexInt:&value]) {

c = value;

} else {

valid = NO;

}

} else {

// decimal

int value;

if ([scanner scanInt:&value] && value >= 0) {

c = value;

} else {

valid = NO;

}

}

if (![scanner scanString:@";" intoString:NULL]) {

// not ;-terminated, bail out and emit the whole entity

valid = NO;

}

} else {

if (![scanner scanUpToString:@";" intoString:&temp]) {

// &; is not a valid entity

valid = NO;

} else if (![scanner scanString:@";" intoString:NULL]) {

// there was no trailing ;

valid = NO;

} else if ([temp isEqualToString:@"amp"]) {

c = '&';

} else if ([temp isEqualToString:@"quot"]) {

c = '"';

} else if ([temp isEqualToString:@"lt"]) {

c = '

} else if ([temp isEqualToString:@"gt"]) {

c = '>';

} else {

// unknown entity

valid = NO;

}

}

if (!valid) {

// we errored, just emit the whole thing raw

[results appendString:[input substringWithRange:NSMakeRange(savedLocation, [scanner scanLocation]-savedLocation)]];

} else {

[results appendFormat:@"%C", c];

}

}

}

return results;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值