用标记分隔的分解列表


Explode list separated by tags

我有一个如下分隔的列表:

<!--start-->
item 1
<!--end-->
<!--start-->
item 2
<!--end-->

我需要做一个数组,第一个var是项目1,第二个项目2,依此类推…

我该怎么做?

$string = .... <your data>
$array = explode('<!--start-->'n', $string);
$final = array();
foreach ($array as $line) {
   $final[] = str_replace('<!--end-->'n', '', $line);
}
echo "<pre>";
print_r($final);

这会给你想要的东西。

看看preg_split()函数。

我的主张:

$str = "<!--start-->
item 1
<!--end-->
<!--start-->
item 2
<!--end-->";
$in = explode(PHP_EOL, $str);
function filter($ell) {
    if (strpos($ell, '<!--') !== 0){
        return true;
    }
    return false;
}
$arr = array_filter($in, 'filter');
var_dump($arr);

假设列表在$input:中

// remove the start tag and add a newline at the end
$input = str_replace("<!--start-->'n", "", $input . "'n");
// break the list into an array (the last item will be an empty string)
$output = explode("'n<!--end-->'n", $input);
// remove the empty item at the end
unset($output[count($output) - 1]);