Original geschrieben von mrhappiness
Versuch das mal, wenn's klappt, schaffst du den Rest alleine *g*
PHP-Code:
function parse($code, &$array) {
$pattern = '%\\<\\!-- BEGIN (.*?) --\\>(.*?)\\<\\!-- END (\\\\1) --\\>%s';
preg_match_all($pattern,
$code,
$matches,
PREG_SET_ORDER);
foreach ($matches as $match) {
$parts = preg_split($pattern, $match[2]);
preg_match_all($pattern, $match[2], $blocks);
$blocks = $blocks[0];
$array[] = array('name' => $match[1]);
$index = count($array) - 1;
foreach ($parts as $key => $value) {
if (!empty($value))
$array[$index][] = $value;
if (isset($blocks[$key]))
parse($blocks[$key], $array[$index][]);
}
if (isset($blocks[++$key]))
parse($blocks[$key], $array[$index][]);
}
}
PHP-Code:
function getblocks ($string,&$array)
{
if( preg_match_all('%<!-- BEGIN (.+) -->(((?R)|(.*))*)<!-- END -->%isU',
$string,
$matches) )
foreach ($matches as $key => $var)
if ( !empty($matches[1][$key]) )
{
$array[] = array('name' => $matches[1][$key]);
getlist($matches[2][$key], $array[count($array) - 1]['nested']);
if ( count($array[count($array) - 1]['nested']) == 0 )
unset($array[count($array) - 1]['nested']);
}
}
Kommentar