0

次のように区切られたリストがあります。

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

最初の変数が項目 1、2 番目の項目が 2 などの配列を作成する必要があります...

それ、どうやったら出来るの?

4

4 に答える 4

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);

それはあなたが探しているものを提供します。

于 2012-07-26T22:53:04.377 に答える
0

リストが次の場所にあると仮定します$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]);
于 2012-07-26T22:55:34.923 に答える
0

私の提案:

$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);
于 2012-07-26T22:56:48.493 に答える
0

preg_split() 関数を見てください。

于 2012-07-26T22:53:38.457 に答える