我會親自使用preg_split
擺脫會從最後一個分號發生額外的數組元素的...
$newarray = array();
foreach ($array as $i => $styles):
// Split the statement by any semicolons, no empty values in the array
$styles = preg_split("/;/", $styles, -1, PREG_SPLIT_NO_EMPTY);
// Add the semicolon back onto each part
foreach ($styles as $j => $style) $styles[$j] .= ";";
// Store those styles in a new array
$newarray[$i] = $styles;
endforeach;
編輯:別t將分號加到每行:
$newarray = array();
foreach ($array as $i => $styles):
// Split the statement by any semicolons, no empty values in the array
$newarray[$i] = preg_split("/;/", $styles, -1, PREG_SPLIT_NO_EMPTY);
endforeach;
應該輸出:
Array(
[0] => width: 650px;
[1] => border: 1px solid #000;
)
不像爆炸,這應該輸出:
Array(
[0] => width: 650px;
[1] => border: 1px solid #000;
[2] => ;
)
ops,對不起,如果我想徹底忽略「;」怎麼辦? – arkchong 2010-07-15 03:11:36
如果您不希望分號剛刪除那裏的第二個foreach語句。 – animuson 2010-07-15 03:12:22