Deniz Celebi
Deniz Celebi

Reputation: 908

Java replace in string html img tags

I am parsing a json feed and displaying its content in a android webview. Everything works great. But now i want to hide all img tags in that android webview.

The problem is that the content in the webview is showen dynamically, that means i don't know the img tag parameters. So i need something to replace everything in that string that begins with

     <img ...  > 

and ends with

   </img>

How can I do that?

Upvotes: 2

Views: 5263

Answers (2)

Deniz Celebi
Deniz Celebi

Reputation: 908

Because none of these answeres worked for me, i finally decided to use jsoup to filter and remove all img tags.

using jsop was a little bit complex but it worked!!

EDIT Here is an example java code

String content = "<h1>title</h1><img src="http://..."></img>";
if(content != null) {
    Document document = Jsoup.parse(content);
    document.select("img").remove();
    content = document.toString();
}

Upvotes: 7

Mifmif
Mifmif

Reputation: 3190

If you handle the String yourself and you set it to the webview, then suppose your content is in a String called oldWebViewContent , and try this :

String webViewContentExcludeImage = oldWebViewContent.replaceAll("<img .*?</img>","");

Upvotes: 1

Related Questions