[development] preg_match bug or regex help needed?

Doug Green douggreen at douggreenconsulting.com
Mon Dec 31 00:49:09 UTC 2007


I'm pretty good with regex, but I'm beating my head against the wall
with this one, and wondering if there's a bug in php.

Any help is much appreciated.  (This is for nicedit.)

<?php
  $text = 'style="font-style: italic; font-weight: bold;"';
  $regex = "(\s*([a-z][a-z0-9\-]*)\s*:\s*([a-z][a-z0-9\-]*)\s*;)+";
  if (preg_match('/'. $regex .'+/i', $text, $matches)) {
    print_r($matches);
  }
?>

If the regex is confusing, just think of it simply as ((\w+):(\w+);)+
... with extra spaces and a definition for words that includes dashes.

The regex vars $1 and $2 seem to only put the last matching value in the
matches array.

Array
(
    [0] => font-style: italic; font-weight: bold;
    [1] =>  font-weight: bold;
    [2] => font-weight
    [3] => bold
)

I was hoping that it would also write font-style: italic into the matches.

Thanks,
Doug Green




More information about the development mailing list