我不确定我是否理解你的问题。如果您想接受上面的输入并生成以下内容:
Array
(
[##cat 1##] => Array
(
[0] => element 1
[1] => element 2
[2] => element 3
)
[##cat 2##] => Array
(
[0] => element 1
[1] => element 2
[2] => element 3
)
[##cat 3##] => Array
(
[0] => element 1
[1] => element 2
[2] => element 3
)
)
尝试这个:
$input_text =<<<INPUT_TEXT
##cat 1##
**
element 1
element 2
element 3
**
##cat 2##
**
element 1
element 2
element 3
**
##cat 3##
**
element 1
element 2
element 3
**
INPUT_TEXT;
$output = array();
$current = NULL;
foreach( explode( "\n", $input_text ) as $line )
{
$line = trim( $line );
if( empty( $line ) )
{
continue;
}
if( substr( $line, 0, 2 ) == '##' )
{
$current = trim( $line );
continue;
}
if( substr( $line, 0, 2 ) == '**' )
{
continue;
}
if( !empty( $current ) )
{
$output[$current][] = $line;
}
}
print_r( $output );