从NSString中删除HTML标签[重复]

时间:2022-08-27 16:28:38

Possible Duplicate:
Remove HTML Tags from an NSString on the iPhone

可能重复:从iPhone上的NSString中删除HTML标记

I have a method that i use to remove HTML tags from NSString(i made a category with that):

我有一个方法,我用来从NSString中删除HTML标签(我用它做了一个类别):

-(NSString *) stringByStrippingHTML {
    NSRange r;
    NSString *s = [[self copy] autorelease];
    while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
        s = [s stringByReplacingCharactersInRange:r withString:@""];
    return s;
}

The issue is that i want to remove all HTML tags except the <br> tags, can i able to do it?

问题是,我想删除除标签以外的所有HTML标签,我能够做到吗?

2 个解决方案

#1


1  

You can tweak as

你可以调整为

First replce <br/> with *br/* then replace all html tags and then again replace *br/* to <br/>.

首先用* br / *复制,然后替换所有html标签,然后再将* br / *替换为

Or, you can use conditional replacing .. if read tag is br continue else replace.

或者,您可以使用条件替换..如果读取标记是br继续其他替换。

#2


0  

Before calling stringByReplacingCharactersInRange: make sure the tag is not <br />.

在调用stringByReplacingCharactersInRange之前:确保标签不是

#1


1  

You can tweak as

你可以调整为

First replce <br/> with *br/* then replace all html tags and then again replace *br/* to <br/>.

首先用* br / *复制,然后替换所有html标签,然后再将* br / *替换为

Or, you can use conditional replacing .. if read tag is br continue else replace.

或者,您可以使用条件替换..如果读取标记是br继续其他替换。

#2


0  

Before calling stringByReplacingCharactersInRange: make sure the tag is not <br />.

在调用stringByReplacingCharactersInRange之前:确保标签不是