当前位置:编程学习 > wap >>

iphone 开发之过滤html标签

过滤字符串中的html标签的方法:


[cpp]
- (NSString *)filterHtmlTag:(NSString *)originHtmlStr{ 
    NSString *result = nil; 
    NSRange arrowTagStartRange = [originHtmlStr rangeOfString:@"<"]; 
    if (arrowTagStartRange.location != NSNotFound) { //如果找到  
        NSRange arrowTagEndRange = [originHtmlStr rangeOfString:@">"]; 
//        NSLog(@"start-> %d   end-> %d", arrowTagStartRange.location, arrowTagEndRange.location);  
//        NSString *arrowSubString = [originHtmlStr substringWithRange:NSMakeRange(arrowTagStartRange.location, arrowTagEndRange.location - arrowTagStartRange.location)];  
        result = [originHtmlStr stringByReplacingCharactersInRange:NSMakeRange(arrowTagStartRange.location, arrowTagEndRange.location - arrowTagStartRange.location + 1) withString:@""]; 
       // NSLog(@"Result--->%@", result);  
        return [self filterHtmlTag:result];    //递归,过滤下一个标签  
    }else{ 
        result = [originHtmlStr stringByReplacingOccurrencesOfString:@" " withString:@""];  // 过滤 等标签  
        //result = [originHtmlStr stringByReplacingOccurrencesOf  ........  
    } 
    return result; 

- (NSString *)filterHtmlTag:(NSString *)originHtmlStr{
    NSString *result = nil;
    NSRange arrowTagStartRange = [originHtmlStr rangeOfString:@"<"];
    if (arrowTagStartRange.location != NSNotFound) { //如果找到
        NSRange arrowTagEndRange = [originHtmlStr rangeOfString:@">"];
//        NSLog(@"start-> %d   end-> %d", arrowTagStartRange.location, arrowTagEndRange.location);
//        NSString *arrowSubString = [originHtmlStr substringWithRange:NSMakeRange(arrowTagStartRange.location, arrowTagEndRange.location - arrowTagStartRange.location)];
        result = [originHtmlStr stringByReplacingCharactersInRange:NSMakeRange(arrowTagStartRange.location, arrowTagEndRange.location - arrowTagStartRange.location + 1) withString:@""];
       // NSLog(@"Result--->%@", result);
        return [self filterHtmlTag:result];    //递归,过滤下一个标签
    }else{
        result = [originHtmlStr stringByReplacingOccurrencesOfString:@" " withString:@""];  // 过滤 等标签
        //result = [originHtmlStr stringByReplacingOccurrencesOf  ........
    }
    return result;
}

 

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,