Hello forums !!
I would like to know the closing tags for following strings using php

$opening = '<div class="xxy">
<ul id="aab">
<li>';
$closing  = ? //should be </li></ul></div>

How to accomplish this using regular expression, any ideas ??

    I would like to say that you should do it yourself. Not because it is not possible in regex, but because there are tags that should not have closing tags. And then you have to handle exceptions as well in regex. As an example:

    $opening = '<div class="xxy">
    <ul id="aab">
    <li>
    <img src="someting">
    <br>
    <br/>';
    $closing  = ? //should be </li></ul></div> 
    $closing  = ? //would be </br></br></img></li></ul></div>, but that is not correct.
    

    Btw, why do you want regex to do it in the first place? It's not hard to do it yourself.

      I wouldn't do this with regular expressions, the dang things are hard enough to work with at the best of times, without trying to get them to do stuff like this! You could just use a regular array as a sort of stack, and process the tags one by one. You can use a small regular expression to extract one tag at a time like this:

      /\<([a-z]+)[\ \"a-z0-9\/]\>/i

      The regex will need tweaking depending on what will exist in a given tag.

      This will recognise a tag (use the $matches array to extract the tag name found in the brackets), and using a stack you can then close of the tags as you need. However, if your HTML code is going to contain any closing tags, you'll need to look out for these.

      If your code is meant to be used as some sort of valid code fixer, then you probably have a major headache infront of you.

        If it was a complete HTML page you could use the DOM and perhaps tidy as well to fix up broken HTML; but neither is too good with working on fragments because HTML is so sloppy to begin with.

          Write a Reply...