You could theoretically do that in one expression with a lot of alternative, but I strongly advise against that as it would be very hard to read and a major PITA to maintain.
Instead, I’d do something like this:
$post='[img float=center width=10 height=20]test.gif[/img]';
$src=$float='';
$width=$height=0;
if (preg_match('~\\[img([^\\]]*)\\]([^\\]]*)\\[\\/img\\]~i', $post, $matches)) {
$src=$matches[2];
if (strlen($matches[0])) {
$attribs=$matches[0];
if (preg_match('~width=(\\d*)~i', $attribs, $matches)) {
$width=$matches[1];
}
if (preg_match('~height=(\\d*)~i', $attribs, $matches)) {
$height=$matches[1];
}
if (preg_match('~float=(left|center|right)~i', $attribs, $matches)) {
$float=$matches[1];
}
}
}
var_dump($src);
var_dump($width);
var_dump($height);
var_dump($float);
NB: This will match at most one image in the post. If people are allowed multiple images, take a look at [fphp]preg_match_all[/fphp]
$images=parse_bb($code, 'img');
$replace=array();
foreach($images as $image) {
$newImage= // do whatever you want to convert the image to html using $image['content'] and $image['attributes']
$replace[ $image['input'] ]=$newImage;
}
$post=str_replace(array_keys($replace), array_values($replace), $post);
Okay, not quite, since it can only take on of each tag (which is also why my last snippet won’t work at the moment), but it’s rather trivial to extend that to multiple using [fphp]preg_replace_all[/fphp] instead of preg_replace.