Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Removing specific html element with any given value

Last post 07-03-2009, 6:56 PM by Aussie Susan. 2 replies.
Sort Posts: Previous Next
  •  07-03-2009, 5:52 AM 54532

    Removing specific html element with any given value

    Hi,

    I have a HTML line like this:

    <img src="image.jpg" width="200" />

    I'm using PHP and would like to strip out the width element that may contain any given number.

    I currently have this code:

    $sDescription = preg_replace("/[0-9]/", "", $sDescription);

    but it only removes numbers from the html and if the image name contains numbers it'll take them out as well.

    Can anyone help me ammend that code?

     Thanks.

  •  07-03-2009, 2:23 PM 54537 in reply to 54532

    Re: Removing specific html element with any given value

    I'd use an HTML parser. I've heard good things about: http://simplehtmldom.sourceforge.net/
  •  07-03-2009, 6:56 PM 54538 in reply to 54532

    Re: Removing specific html element with any given value

    My experience is the same as that of prometheuzz - HTML is almost always best handled by the HTML DOM.

    The problem with your pattern is that it looks for a single digit and will replace it with a null string. Therefore your code will remove all digits from the text.

    If you want a pattern that will do what you said - "to strip out the width element" - then you need a pattern that will do what you actually need; something like:

    width="\d*"

    This will start the match with the "width=" keyword, match the first double-quote, any digits that follow and then the trailing double quote.

    Remember that you will need to modify the pattern to make sure that it is recognised correctly by the regex function. Specifically you will need to add in the delimiters (as in your pattern) and, because there are double-quotes in the pattern which is specified within a double-quoted string, you will need to escape them in whatever way your programming language requires.

    A final word of caution: this pattern will only work on text such as your example. It will not work if the number is not surrounded by double-quotes (which I think is quite common) and it will also remove any "width=..." text form anywhere within the text (especially if it is more than just the single tag you have shown).

    Susan 

View as RSS news feed in XML