[英]iOS: Strip <img…> from NSString (a html string)
So I have an NSString
which is basically an html
string with all the usual html
elements.所以我有一个
NSString
,它基本上是一个包含所有常用html
元素的html
字符串。 The specific thing I would like to do is to just strip it from all the img
tags.我想做的具体事情就是从所有
img
标签中删除它。 The img
tags may or may not have max-width, style or other attributes so I do not know their length up front. img
标签可能有也可能没有最大宽度、样式或其他属性,所以我不知道它们的长度。 They always end with />
他们总是以
/>
结尾
How could I do this?我怎么能这样做?
EDIT: Based on nicolasthenoz
's answer, I came up with a solution that requires less code:编辑:基于
nicolasthenoz
的回答,我想出了一个需要更少代码的解决方案:
NSString *HTMLTagss = @"<img[^>]*>"; //regex to remove img tag
NSString *stringWithoutImage = [htmlString stringByReplacingOccurrencesOfRegex:HTMLTagss withString:@""];
You can use the NSString
method stringByReplacingOccurrencesOfString
with the NSRegularExpressionSearch
option:您可以将
NSString
方法stringByReplacingOccurrencesOfString
与NSRegularExpressionSearch
选项一起使用:
NSString *result = [html stringByReplacingOccurrencesOfString:@"<img[^>]*>" withString:@"" options:NSCaseInsensitiveSearch | NSRegularExpressionSearch range:NSMakeRange(0, [html length])];
Or you can also use the replaceMatchesInString
method of NSRegularExpression
.或者你也可以使用
replaceMatchesInString
的方法NSRegularExpression
。 Thus, assuming you have your html in a NSMutableString *html
, you can:因此,假设您将 html 放在
NSMutableString *html
,您可以:
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<img[^>]*>"
options:NSRegularExpressionCaseInsensitive
error:nil];
[regex replaceMatchesInString:html
options:0
range:NSMakeRange(0, html.length)
withTemplate:@""];
I'd personally lean towards one of these options over the stringByReplacingOccurrencesOfRegex
method of RegexKitLite
.我个人的偏向这些选项在一个
stringByReplacingOccurrencesOfRegex
的方法RegexKitLite
。 There's no need to introduce a third-party library for something as simple as this unless there was some other compelling issue.除非有其他一些引人注目的问题,否则没有必要为这样简单的事情引入第三方库。
Use a regular expression, find the matchs in your string and remove them !使用正则表达式,找到字符串中的匹配项并删除它们! Here is how
这是如何
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<img[^>]*>"
options:NSRegularExpressionCaseInsensitive
error:nil];
NSMutableString* mutableString = [yourStringToStripFrom mutableCopy];
NSInteger offset = 0; // keeps track of range changes in the string due to replacements.
for (NSTextCheckingResult* result in [regex matchesInString:yourStringToStripFrom
options:0
range:NSMakeRange(0, [yourStringToStripFrom length])]) {
NSRange resultRange = [result range];
resultRange.location += offset;
NSString* match = [regex replacementStringForResult:result
inString:mutableString
offset:offset
template:@"$0"];
// make the replacement
[mutableString replaceCharactersInRange:resultRange withString:@""];
// update the offset based on the replacement
offset += ([match length] - resultRange.length);
}
You can use below function in Swift 4,5:您可以在 Swift 4,5 中使用以下函数:
func filterImgTag(text: String) -> String{
return text.replacingOccurrences(of: "<img[^>]*>", with: "", options: String.CompareOptions.regularExpression)
}
Hope it can help you all!希望能帮到大家! comment below if it work for you.
如果它对你有用,请在下面评论。 Thanks.
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.