3

I have some html string that is coming from telerik radeditor, it may contain images tags with width and height. I want to remove those width and height properties. How can I do this in code using regex or something else in asp.net?

hotcoder
  • 3,176
  • 10
  • 58
  • 96

4 Answers4

2

Two Regex replace statements will do the job pretty well:

str = Regex.Replace(str, @"(<img[^>]*?)\s+height\s*=\s*\S+",
        "$1", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"(<img[^>]*?)\s+width\s*=\s*\S+",
        "$1", RegexOptions.IgnoreCase);

(This is a C# snippet - not sure if ASP.NET is the same)

ridgerunner
  • 33,777
  • 5
  • 57
  • 69
1

Not sure I understand the question, but why not just omit them in the first place instead of trying to remove them?

In your ASPX file....

<img src="images/myimage.jpg">

And for the love of God, don't try to strip them out with Regex.

JohnFx
  • 34,542
  • 18
  • 104
  • 162
1

There are a lot of mentions regarding not to use regex when parsing HTML, so you could use e.g. Html Agility Pack for this:

HtmlDocument document = new HtmlDocument();
document.LoadHtml(html);

var images = document.DocumentNode.SelectNodes("//img");
foreach (HtmlNode image in images)
{
    if (image.Attributes["width"] != null)
    {
        image.Attributes["width"].Remove();
    }
    if (image.Attributes["height"] != null)
    {
        image.Attributes["height"].Remove();
    }
}

this will remove width and height attribute from images in your html.

Community
  • 1
  • 1
Oleks
  • 31,955
  • 11
  • 77
  • 132
-2
str = Regex.Replace(str, @"(<img[^>]*?)\s+height\s*=\s*\S+",
        "$1", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"(<img[^>]*?)\s+width\s*=\s*\S+",
        "$1", RegexOptions.IgnoreCase);
Karim
  • 18,347
  • 13
  • 61
  • 70
dsf
  • 1