preg_match_all to parse an xml-like attribute string

Posted by Rob on Stack Overflow See other posts from Stack Overflow or by Rob
Published on 2010-03-28T18:27:54Z Indexed on 2010/03/28 18:33 UTC
Read the original article Hit count: 236

I have a string like so:

option_alpha="value" option_beta="some other value" option_gamma="X" ...etc.

I'm using this to parse them into name & value pairs:

preg_match_all("/([a-z0-9_]+)\s*=\s*[\"\'](.+?)[\"\']/is", $var_string, $matches)

Which works fine, unless it encounters an empty attribute value:

option_alpha="value" option_beta="" option_gamma="X"

What have I done wrong in my regex?

© Stack Overflow or respective owner

Related posts about php

Related posts about preg-match