This code gives the last day name. This means that if $val
contains only one day, this day will be displayed. You can easily change this behavior to get only a result when $val
contains two days by removing the ?
. If you want to deal with more days, replace ?
with *
.
I think it is more useful to flip the associative array, to get the day name at the end and to use array_keys
to build the day subpattern ($dayPat
).
$val = '8:55pm[Tues-Thurs Only]';
$daysCorr = array(
'Mon' => 'Monday',
'Tues' => 'Tuesday',
'Wed' => 'Wednesday',
'Thurs' => 'Thursday',
'Fri' => 'Friday',
'Sat' => 'Saturday',
'Sun' => 'Sunday');
$dayPat = implode('|', array_keys($daysCorr));
$pattern = <<<EOD
~
^
\d{1,2} : \d{2} [ap]m
\[
(?:
(?: $dayPat ) # first day
(?: - | [ ]&[ ] ) # day delimiter
)? # ? makes the first day optional, remove it if not needed
(?<day> $dayPat )
[ ] # Since the x modifier is used (free space mode),
# you must use [ ] to write a literal space
Only ]
\z # end of the string
~x
EOD;
if (preg_match($pattern, $val, $match))
echo $daysCorr[$match['day']];