[development] preg_match bug or regex help needed?

Bevan Rudge bevan at civicactions.com
Mon Dec 31 01:15:55 UTC 2007


Excuse the html email.  It means I can highlight things and hence
communicate more clearly.

First, I think you have an extra '+' character, which results in a double
plus '++' being passed into preg_match()

$regex = "(\s*([a-z][a-z0-9\-]*)\s*:\s*([a-z][a-z0-9\-]*)\s*;)+";
> if (preg_match('/'. $regex .'+/i', $text, $matches)) {...
>

which calls
preg_match('/(\s*([a-z][a-z0-9\-]*)\s*:\s*([a-z][a-z0-9\-]*)\s*;)++/i',
'style="font-style: italic; font-weight: bold;"', $matches))

Nevertheless, that's not the cause, because removing one or both plusses
doesn't solve the problem.

The thing that strikes me as odd with this regex is the wrapping of the
outer parenthesis, in a multiplier.  I would instead look for a way of
repeating the smaller regex on the string until the string is finished.
This code;

$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_all('/'. $regex .'/i', $text, $matches, PREG_SET_ORDER)) {
  print_r($matches);
}

returns this;

> Array
> (
>     [0] => Array
>         (
>             [0] => font-style: italic;
>             [1] => font-style
>             [2] => italic
>         )
>
>     [1] => Array
>         (
>             [0] =>  font-weight: bold;
>             [1] => font-weight
>             [2] => bold
>         )
>
> )
>
> Which I think is more similar to what you are seeking?

Also, I expect there is probably a php library that does this with simpler
function calls than writing you're own ajax.  Possibly as part of or related
to the DOM class and related classes http://nz.php.net/manual/en/ref.dom.php

Cheers,
Bevan/

-- 
Drupal.geek.nz | Gtalk bevan at lucion.co.nz | YIM rudgy_m_nz | .Mac/AOL
b.rudge | skype b.rudge | Twitter.com/BevanR
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20071231/ad797a95/attachment.htm 


More information about the development mailing list