On 1/23/2011 3:33 PM, Jonathan Street wrote:
I think you're over-complicating the problem in trying to construct a
regex that does everything.  I suspect it would be easier to search for
\[mediaspace(.+?)\] and then use explode on space to separate out each
of the attribute=value pairs and then explode again on = to separate
attribute from value.

This is certainly a valid approach, but a regex would let you do things that an explode would not.

What I would do is use two regexes. The first would find the initial tag, and not parse it, ala:

\[mediaspace\s(.+?)\]

The second regex would match against that single submatch ($1) using a preg_match_all(). preg_match_all()'s default configuration makes it easily loopable. You'd do something like this:

// $tag_match is the incoming match of the above regex
$params = array(/* default values here */);
preg_match_all('/(\w+)\s*=\s*(\S+)/i', $tag_match[1], $param_matches);
foreach($param_matches as $param_match) {
        $params[$param_match[1]] = $param_match[2];
}

Then $params would contain your parameters.

What this would let you do that an explode wouldn't is allow you to put spaces between your parameter names/values and the equals signs, which is more forgiving of the user. You can even enhance this regex to include quoted params so that you could use spaces in your values, in case your filenames or labels need them:

'/(\w+)\s*=\s*("[^"]+"|\S+)/i'

Obviously, the quotes would be included in the value string, so you'd need to strip them if they were present. But this would be quite effective.

Owen

--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at http://groups.google.com/group/habari-dev

Reply via email to