2013-08-05 109 views
0

我的PHP的問題是如下內容的數組:PHP - 創建具有爆炸

我有這個字符串:$days="Monday* Tuesday* Wednesday*"

我想創建一個這樣的數組:$d[1]="Monday"; $d[2]="Tuesday"; $d[3]="Wednesday"

我感覺到,溶液流通過explode(*, $days)但我無法得到進一步

謝謝您的幫助:)

+0

如何處理$ days字符串的來源以使此easer – 2013-08-05 22:21:38

回答

1
$days="Monday* Tuesday* Wednesday*"; 
$d=explode('* ', $days); 

或者最後一個字的結束

$days="Monday* Tuesday* Wednesday*"; 
$d=explode(' ', str_replace('*','',$days)); 

爲排除 「*」。

1
explode('* ', trim($days, '*')); 
0
$days="Monday* Tuesday* Wednesday*"; 
$exploded = explode('* ',substr($days, 0, -1)); 
print_r($exploded); 

輸出:

Array 
(
    [0] => Monday 
    [1] => Tuesday 
    [2] => Wednesday 
) 
1

你應該使用:

explode("* ", $days) 

但IMO,我會用這樣的昏迷:

$days = "Monday,Tuesday,Wednesday"; 
$days = explode(",", $days); 
echo $days[0]; // => Monday 
echo $days[1]; // => Tuesday 
0
<?php 
$days = 'Monday* Tuesday* Wednesday*'; 
$d = array_filter(explode('* ', $days));