iOS:从NSString(一个html字符串)</img ...>中删除<img ...>

时间:2012-09-19 14:28:40

标签: html objective-c ios image

所以我有一个NSString,它基本上是一个html字符串,包含所有常见的html元素。我想要做的具体事情是从所有img代码中删除它。 img标签可能有也可能没有max-width,style或其他属性,因此我不知道它们的长度。它们始终以/>

结尾

我怎么能这样做?

编辑:根据nicolasthenoz的回答,我想出了一个需要更少代码的解决方案:

NSString *HTMLTagss = @"<img[^>]*>"; //regex to remove img tag
NSString *stringWithoutImage = [htmlString stringByReplacingOccurrencesOfRegex:HTMLTagss withString:@""]; 

3 个答案:

答案 0 :(得分:14)

您可以将NSString方法stringByReplacingOccurrencesOfStringNSRegularExpressionSearch选项一起使用:

NSString *result = [html stringByReplacingOccurrencesOfString:@"<img[^>]*>" withString:@"" options:NSCaseInsensitiveSearch | NSRegularExpressionSearch range:NSMakeRange(0, [html length])];

或者您也可以使用NSRegularExpression的{​​{3}}方法。因此,假设您的html位于NSMutableString *html,您可以:

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<img[^>]*>"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:nil];

[regex replaceMatchesInString:html
                      options:0
                        range:NSMakeRange(0, html.length)
                 withTemplate:@""];

我个人倾向于使用replaceMatchesInString stringByReplacingOccurrencesOfRegex方法中的其中一个选项。除非有其他令人信服的问题,否则没有必要为这样简单的事情引入第三方库。

答案 1 :(得分:4)

使用正则表达式,找到字符串中的匹配项并删除它们! 这是如何

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);
}

答案 2 :(得分:0)

您可以在Swift 4,5中使用以下功能:

2020-04-29T08:14:12.493652+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2020-04-29T08:14:12.493652+00:00 app[web.1]: self.callable = self.load()
2020-04-29T08:14:12.493653+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
2020-04-29T08:14:12.493653+00:00 app[web.1]: return self.load_wsgiapp()
2020-04-29T08:14:12.493653+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
2020-04-29T08:14:12.493653+00:00 app[web.1]: return util.import_app(self.app_uri)
2020-04-29T08:14:12.493654+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 358, in import_app
2020-04-29T08:14:12.493654+00:00 app[web.1]: mod = importlib.import_module(module)
2020-04-29T08:14:12.493654+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
2020-04-29T08:14:12.493655+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2020-04-29T08:14:12.493655+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2020-04-29T08:14:12.493656+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2020-04-29T08:14:12.493656+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
2020-04-29T08:14:12.493657+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
2020-04-29T08:14:12.493657+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 678, in exec_module
2020-04-29T08:14:12.493658+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2020-04-29T08:14:12.493658+00:00 app[web.1]: File "/app/server.py", line 90, in <module>
2020-04-29T08:14:12.493658+00:00 app[web.1]: startServer()
2020-04-29T08:14:12.493659+00:00 app[web.1]: File "/app/server.py", line 87, in startServer
2020-04-29T08:14:12.493659+00:00 app[web.1]: server = HTTPServer(serverAdd, Server)
2020-04-29T08:14:12.493659+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/socketserver.py", line 456, in __init__
2020-04-29T08:14:12.493660+00:00 app[web.1]: self.server_bind()
2020-04-29T08:14:12.493660+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/http/server.py", line 136, in server_bind
2020-04-29T08:14:12.493660+00:00 app[web.1]: socketserver.TCPServer.server_bind(self)
2020-04-29T08:14:12.493661+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/socketserver.py", line 470, in server_bind
2020-04-29T08:14:12.493661+00:00 app[web.1]: self.socket.bind(self.server_address)
2020-04-29T08:14:12.493661+00:00 app[web.1]: OSError: [Errno 98] Address already in use
2020-04-29T08:14:12.493733+00:00 app[web.1]: [2020-04-29 08:14:12 +0000] [11] [INFO] Worker exiting (pid: 11)

希望它可以帮助大家!如果对您有用,请在下面评论。谢谢。