次のように区切られたリストがあります。
<!--start-->
item 1
<!--end-->
<!--start-->
item 2
<!--end-->
最初の変数が項目 1、2 番目の項目が 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);
それはあなたが探しているものを提供します。
リストが次の場所にあると仮定します$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]);
私の提案:
$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);
preg_split() 関数を見てください。