preg_match regex not quite working

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DH

    preg_match regex not quite working

    I need to parse some HTML tags and display the style classes, and have
    it partly working, but need some regex / preg_match advise.

    If the tag is <td class="red" colspan="1"> I can display "td.red"

    If the tag is <td colspan="1" class="red"> my regex doesn't display the
    desired "td.red" ... I can't figure out how to skip over colspan="1" if
    the class doesn't immediately follow the HTML tag name.


    $lines = explode("\n", $html);

    foreach($lines as $line){

    if(stristr($lin e, '<') && strstr($line, 'class=')){

    if(preg_match("/<+([a-z]+) class=\"+([_a-z0-9-]+)\"/", $line,
    $matches)){

    echo
    '<b>'.htmlspeci alchars($matche s[1].'.'.$matches[2]).'</b><br><br>'."\n ";

    }

    }

    }


    TIA.
  • jblanch

    #2
    Re: preg_match regex not quite working

    Okay, i ran the script, and i noticed that it wasn't picking up any
    spaces between the differen't rules, so it would pick up [space]class=
    but it wont pick up [space]somthing[space]class, or maybe not even
    just somthing[space]class,

    when i modifed your regex code, i used the (.*) after the [a-z] so it
    would take anything else in the code and put it into the matches. What
    this means, is that it will match everything between the original tag
    name and the class.

    Ex:
    <td colspan="1" rawr="woo" class="red">
    will return
    Array
    (
    [0] => <td colspan="1" rawr="woo" class="red"
    [1] => td
    [2] => colspan="1" rawr="woo"
    [3] => red
    )
    (directly from php code)

    and also, any thing after the class="red" does NOT match at all, so
    your final result will be

    echo
    '<b>'.htmlspeci alchars($matche s[1].'.'.$matches[count($matches)-1]).'</b><br><br>'."\n ";
    instead of just $matches[2];

    Comment

    • jblanch

      #3
      Re: preg_match regex not quite working

      mind if i ask what this is for too? i'm kinda interested to see what
      you would/could do with this.

      Comment

      • DH

        #4
        Re: preg_match regex not quite working

        jblanch wrote:[color=blue]
        > mind if i ask what this is for too? i'm kinda interested to see what
        > you would/could do with this.[/color]

        Thanks for the reply. There are 80+ un-documented stylesheet classes in
        a form generator named phpMyEdit, which I use extensively for admin
        forms in web site projects.

        Various styles appear in various page modes (add, change, copy, delete,
        view, list, etc.). Every time I think I've tracked down all the
        available style classes, something new is added, or else I find a
        previously overlooked class.

        In order to create a dynamic stylesheet (style.php) that evaluates the
        current page mode (add, change, etc.) before defining colors and cell
        borders, I need to make sure I've gathered ALL the relevant style classes.

        In the next stable release of phpMyEdit (or currently in CVS) there will
        be $_REQUEST vars that indicate with certainty the currently active page
        mode (or which button was last clicked). It's likely there will also be
        some new style classes, and I don't want to have to gather them
        individually by hand (again).



        IMHO, a better form generator does not exist, and the amount of brain
        damage required to write something comparable to phpMyEdit is a waste of
        time.

        Thanks again for the regex help.

        Comment

        • jblanch

          #5
          Re: preg_match regex not quite working

          Awsome, glad i could help. If theres anything else i'm bored enough to
          help.

          Comment

          Working...