C# How to find a 1x1 image tag in HTML

598 Views Asked by At

I would like to know how to find an image tag in a string of HTML that has dimensions of 1x1, basically I am looking for tracking pixels. So for example:

<img src=\"http://somewhere.com\" width=\"1\" height=\"1\" style=\"display:none!important;\">

My end goal is to be able to find this part of the code and remove it from the string.

I have read few a few posts already on how to find all img tags but that is not what I am looking for. I only want the img tags that are 1x1.

Can anyone help with this?

2

There are 2 best solutions below

2
Victor Leontyev On BEST ANSWER

If you just want to remove this img tags from your string, you can do that with regexp:

 string result = Regex.Replace(html, "<img.+?(width|height)=[\"']1[\"'].+?(width|height)=[\"']1[\"'].*>", "", RegexOptions.IgnoreCase);
1
Dmitry Pavlushin On

Not really a very good practice of parsing HTML with regexp, but I guess you colud get all <img> tags with <img.*?> and then do your regular

if (str.IndexOf("width=\"1\"" > 0 && str.IndexOf("height=\"1\"") > 0)

but there is much more to it, and I suggest you take a look at HTML Agility Pack