我在这里提出了一个问题,即结合商店营业时间相似的日子
Combine days where opening hours are similar
现在我需要一些帮助来做相反的事情:收集包含合并日期的字符串并将它们分成单独的日子,并按照当天的顺序将它们存储在一个数组中(Mon first,Sun last)。
例如,这是一组需要转换的字符串(完全相同的格式):
Mon-Fri 11 am - 7:30 pm
Sat 11 am - 6 pm
两个字符串需要组合在一起并转换为此数组:
array[0] = "11am - 7:30pm"
array[1] = "11am - 7:30pm"
array[2] = "11am - 7:30pm"
array[3] = "11am - 7:30pm"
array[4] = "11am - 7:30pm"
array[5] = "11am - 6pm"
array[6] = "" // Blank value if the day Sunday is not in the set of strings that needs conversion
// ideally it should be an associative array, ie. array['mon'] = "11am - 7:30pm"
不过,我正在使用Codeigniter PHP框架和jQuery。
更新
我现在必须拆分包含逗号的文本字符串!
Mon 9 pm - 1 am
Tue-Wed, Sun 7:30 pm - 1 am
Thu-Sat 7:30 pm - 2 am
产生的数组:
Array
(
[Mon] => 9 pm - 1 am
[Tue] =>
[Wed] =>
[Thu] => 7:30 pm - 2 am
[Fri] => 7:30 pm - 2 am
[Sat] => 7:30 pm - 2 am
[Sun] =>
)
看起来我必须从Tue-Wed
分割Sun
,而不是
Tue-Wed, Sun 7:30 pm - 1 am
到
Tue-Wed 7:30 pm - 1 am
Sun 7:30 pm - 1 am
@Khattam :这段时间:
Mon 9 pm - 1 am
Tue-Wed, Sun 7:30 pm - 1 am
Thu-Sat 7:30 pm - 2 am
给出:
Array
(
[Mon] => 9 pm - 1 am
[Tue] => 7:30 pm - 1 am
[Wed] => 7:30 pm - 1 am
[Thu] => 7:30 pm - 2 am
[Fri] => 7:30 pm - 2 am
[Sat] => 7:30 pm - 2 am
[Sun] =>
[
] =>
)
答案 0 :(得分:1)
使用RegExp
来解析日期,然后使用相同值对集合中的每一天进行迭代。
var hrsCondensed = ["Mon-Fri 11 am - 7:30 pm", "Sat 11 am - 6 pm"];
var hrsExpanded = ["","","","","","",""];
var days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
$.each(hrsCondensed, function()
{
if (/([a-z]{3})-([a-z]{3}) (.+)/i.test(this))
{
var start = RegExp.$1;
var end = RegExp.$2;
var hrs = RegExp.$3;
var i = $.inArray(start, days);
hrsExpanded[i] = hrs;
while(days[i] != end)
{
if (++i > 6) i = 0;
hrsExpanded[i] = hrs;
}
}
else
{
var day = this.substr(0, 3);
var hrs = this.substr(4);
var i = $.inArray(day, days);
hrsExpanded[i] = hrs;
}
});
答案 1 :(得分:1)
您还可以使用控制器操作,其代码类似于以下内容:
<?php
$days = array(
'Mon',
'Tue',
'Wed',
'Thu',
'Fri',
'Sat',
'Sun'
);
$combined = "Tue-Wed, Sun 7:30 pm - 1 am";
$combined = explode("\n",$combined);
foreach($combined as &$c){
$commaPosition = strpos($c,', ');
if($commaPosition!==false){
array_push($combined,substr($c,$commaPosition+2));
$spacePosition = strpos($c,' ',$commaPosition+2);
$c = substr($c,0,$commaPosition).substr($c,$spacePosition);
}
}
$splitArray = array();
foreach($days as $d){
$splitArray[$d]='';
}
foreach($combined as $c){
$daysRange = explode(' ',$c);
$daysRange = $daysRange[0];
if(strpos($daysRange,'-')===false){
$splitArray[$daysRange] = substr($c,strlen($daysRange)+1);
}
else{
$temp = explode('-',$daysRange);
$dayStart = $temp[0];
$dayStartIndex = array_search($dayStart,$days);
$dayEnd = $temp[1];
$dayEndIndex = array_search($dayEnd,$days);
for($i=$dayStartIndex;$i<=$dayEndIndex;$i++){
$splitArray[$days[$i]] = substr($c,strlen($daysRange)+1);
}
}
}
echo json_encode($splitArray);
然后使用$ .getJSON()来获取数据。
答案 2 :(得分:0)
使用正则表达式!
示例(快速和脏):
<html>
<body>
<script type="text/javascript">
var a = [];
a.push("Mon-Fri 11 am - 7:30 pm");
a.push("Sat 11 am - 6 pm");
var seeker = /(\d{1,2}|\d{1,2}:\d{1,2}) (am|pm)/g;
for (var i = 0; i < a.length; ++i) {
document.write(a[i] + " ==> ");
document.write(a[i].match(seeker) + "<br>");
}
</script>
</body>
</html>
将此代码放入a sandbox,您将获得:
Mon-Fri 11 am - 7:30 pm ==> 11 am,7:30 pm
Sat 11 am - 6 pm ==> 11 am,6 pm
答案 3 :(得分:0)
这应该让你开始。
var dates = ['Mon-Fri 11 am - 7:30 pm','Sat 11 am - 6 pm'];//for testing
var regex = '(\\w{3})-?(\\w{3})?\\s+(.*)';
var week = {'Mon':0, 'Tue':1, 'Wed':2, 'Thu':3, 'Fri':4, 'Sat':5, 'Sun':6};
function foo(){
var datum = [];
re = new RegExp(regex);
for(i in dates){
match = re.exec(dates[i]);
if(match != null){
from = week[match[1]];
to = match[2]? week[match[2]]: from;
tim = match[3];
for(i=from; i<=to; ++i){
datum[i] = tim;
}
}
}
alert(datum);
}