我正在使用WKWebView加载html字符串,某些html字符串的末尾有一些丑陋的图像链接,我想隐藏它们。
css用于隐藏图像,但不起作用。
.article img[src* = "/smilies/"],
.article img[src* = ".feedburner.com/~ff/"],
.article img[src* = ".feedburner.com/~r/"],
.article img[src* = ".feedblitz.com/"]
{
display: none;
}
我想要隐藏的带有feedburner src的示例html字符串:
<div>
<a href="http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Venturebeat?d=yIl2AUoC8zA" border="0"></a> <a href="http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Venturebeat?d=qj6IDK7rITs" border="0"></a> <a href="http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:V_sGLiPBpWU" border="0"></a> <a href="http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/Venturebeat?d=I9og5sOYxJI" border="0"></a> <a href="http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:D7DqB2pKExk" border="0"></a>
</div>
答案 0 :(得分:0)
实现这一目标的快速而肮脏的方法是使用正则表达式。请注意,这对于长HTML文件来说并不理想,因为它不如真正的HTML解析器那么高效。
// The HTML you posted
NSString *HTML = @"<div>\n\t<a href=\"http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:yIl2AUoC8zA\"><img src=\"http://feeds.feedburner.com/~ff/Venturebeat?d=yIl2AUoC8zA\" border=\"0\"></a> <a href=\"http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:qj6IDK7rITs\"><img src=\"http://feeds.feedburner.com/~ff/Venturebeat?d=qj6IDK7rITs\" border=\"0\"></a> <a href=\"http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:V_sGLiPBpWU\"><img src=\"http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:V_sGLiPBpWU\" border=\"0\"></a> <a href=\"http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:I9og5sOYxJI\"><img src=\"http://feeds.feedburner.com/~ff/Venturebeat?d=I9og5sOYxJI\" border=\"0\"></a> <a href=\"http://feeds.feedburner.com/~ff/Venturebeat?a=H9eoOCii8XI:sanX3-jfWnw:D7DqB2pKExk\"><img src=\"http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:D7DqB2pKExk\" border=\"0\"></a>\n</div>";
// A string containing source of the images that you want to delete
NSString *source = @"http://feeds.feedburner.com/~ff/";
// Builds a pattern that matches the tags of the images you want to delete
NSString *pattern = [NSString stringWithFormat:@"<img src=\"%@.+?>", source];
// The actual delete operation
NSString *cleanHTML = [HTML stringByReplacingOccurrencesOfString:pattern
withString:@""
options:NSRegularExpressionSearch
range:NSMakeRange(0, HTML.length)];
// Do what you want with the cleaned HTML (display it, ...)
NSLog(@"%@", cleanHTML);