你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iphone 開發之過濾html標簽

iphone 開發之過濾html標簽

編輯:IOS開發綜合

過濾字符串中的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:@"&nbsp;" withString:@""];  // 過濾&nbsp等標簽  
        //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:@"&nbsp;" withString:@""];  // 過濾&nbsp等標簽
        //result = [originHtmlStr stringByReplacingOccurrencesOf  ........
    }
    return result;
}

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved