regular expression

Posted on 2006-06-05
Last Modified: 2009-12-16
Dear Experts,

A variable contains in fact a whole html-page.
Within that string there are some badly formed xhtml tags (f.e. tags in CAPS, attributes which are unquoted, etc...).
For most of these issues I have found a solution, but for this one not:
<img src="arrow.gif" width="45" height="46" alt="arrow" border="0"> has to become <img src="arrow.gif" width="45" height="46" alt="arrow" border="0" />

I think with the use of regular expression this can be achieved (but it's not my cup of T)

Many thanks in advance.

Question by:gijsbertjr
    LVL 6

    Expert Comment

    preg_replace('@<img([^/>]+)>@s', '<img$1/>', $html_text);
    LVL 6

    Expert Comment

    (That's assuming that you have the whole HTML page stored in variable $html_text.)
    LVL 6

    Expert Comment

    On second thought, use this:

        preg_replace('@<img([^/>]+)>@si', '<img$1/>', $html_text);

    Only difference is that I added an "i" to make it case-insensitive
    LVL 49

    Accepted Solution

    Hoi Gijsbert,

    You can try and use this expression. It is little different from the one soapergem posted, but might be more accurate. You would have to test with both expressions:

      function correct_img($html_text) {
        $preg = '#<img([^>]*(\S))\s*>#sie';
        $repl = '"<img$1".("\2"=="/" ? "" : "/").">"';
        return preg_replace($preg,$repl, $html_text);

    I ran some test with both algoritm. soapergems is mid column, above function is right column. Input is left column:
    <img src="apple">                 <img src="apple"/>                <img src="apple"/>                
    <img src=""/>                     <img src=""/>                     <img src=""/>                    
    <img src=""/ >                    <img src=""/ >                    <img src=""/>                    
    <img src="apple"  >               <img src="apple"  />              <img src="apple"/>                
    <img src="apple"        >         <img src="apple"        />        <img src="apple"/>                
    <img src="apple"  /   >           <img src="apple"  /   >           <img src="apple"  />              
    <IMG src="apple"  />              <IMG src="apple"  />              <img src="apple"  />              
    <IMG src="apple"  />              <IMG src="apple"  />              <img src="apple"  />


    Author Comment

    Dear soapergem and Roonaan,

    First of all my excuses for this late reaction.
    Thanks both for your help.
    I've run some tests with both expressions and came to the same conclusion as Roonaan.

    I'll go with his solution.

    Best regards,


    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now