preg_match regex not quite working

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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 ""

If the tag is <td colspan="1" class="red"> my regex doesn't display the
desired "" ... 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($line, '<') && strstr($line, 'class=')){

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






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.

<td colspan="1" rawr="woo" class="red">
will return
[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

instead of just $matches[2];

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.

Re: preg_match regex not quite working

jblanch wrote:
Quoted text here. Click to load it

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

Thanks again for the regex help.

Re: preg_match regex not quite working

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

Site Timeline