Instead of constructing regexes for each attribute, try using
preg_match_all to find all the strings with bracket something bracket. Then run preg_match_all on each something to find the string equals quotation mark something question mark. Hard to explain without code, so here is an example:
Code: Select all
$elements = array();
preg_match_all('/\[(.+?)\]/', $input, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
preg_match_all('/(\S+?)="(.+?)"/', $match[1], $attrValues, PREG_SET_ORDER);
$attrs = array();
foreach ($attrValues as $m) {
$attrs[$m[1]] = $m[2];
}
$elements[] = $attrs;
}
Code: Select all
Array
(
[0] => Array
(
[src] => http://www.example.com/images/example_image.jpg
[desc] => Image Description
)
)
You'll probably want something more robust that allows newlines, apostrophes instead of quotation marks, allowing for no matches, extracting the ks:img part, etc.