這是我的問題,我有這種類型的字符串如何使用逗號分隔PHP中數組中的單個qoutes列表?
04/19/2017, 04/20/2017, 04/26/2017, 04/28/2017
我要輸出像下面的輸出
'04/19/2017','04/20/2017','04/26/2017','04/28/2017'
這是我的問題,我有這種類型的字符串如何使用逗號分隔PHP中數組中的單個qoutes列表?
04/19/2017, 04/20/2017, 04/26/2017, 04/28/2017
我要輸出像下面的輸出
'04/19/2017','04/20/2017','04/26/2017','04/28/2017'
在不使用爆炸和內爆
$string = "04/19/2017, 04/20/2017, 04/26/2017, 04/28/2017"; $string = "'".str_replace(", ","','",$string)."'";
$string = "04/19/2017, 04/20/2017, 04/26/2017, 04/28/2017";
$array = explode(", ", $string);
$string = "'".implode("','", $array)."'";
爲了您的具體的例子,你可以使用:
$result = preg_replace('%([\d/]+)%sim', '"\1"', $string);
輸出:
"04/19/2017", "04/20/2017", "04/26/2017", "04/28/2017"
Regex的說明:
([\d/]+)
Match the regex below and capture its match into backreference number 1 «([\d/]+)»
Match a single character present in the list below «[\d/]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
A 「digit」 (any decimal number in any Unicode script) «\d»
The literal character 「/」 «/»
"\1"
Insert the character 「"」 literally «"»
Insert the text that was last matched by capturing group number 1 «\1»
Insert the character 「"」 literally «"»
<?php
// dates in a string
$data = '04/19/2017, 04/20/2017, 04/26/2017, 04/28/2017';
// break them apart into array elements where the , is
$dates = explode(',', $data);
/* You then have array of dates you can use/display how you want, ie:: */
foreach($dates as $date){
echo $date. '<br/ >';
}
/* OR select single date */
echo $dates[0];
看看[implode](http://php.net/manual/en/function.implode.php)和[爆炸](http://php.net/manual/en/function.explode .PHP)。 – aynber
你應該粘貼你的代碼 – Michael