1
$list = 'Mon,Tue,Wed,Thrs,Fri,Sat,Sun';

Is there a regex or a function that will work as follows?

1)"Tue"       return string ->"Mon,Wed,Thrs,Fri,Sat,Sun"
2)"Thrs, Mon"     return string ->"Tue,Wed,Fri,Sat,Sun"
3)"Sun,Wed,Fri"       return string ->"Mon,Tue,Thrs,Sat"
4)"Fri"     return string ->"Mon,Tue,Wed,Thrs,Sat,Sun"

Below works fine for removing just one item from the string. What if I want to remove more than one item like above?

$input = 'Wed';
$list = 'Mon,Tue,Wed,Thrs,Fri,Sat,Sun';
    $array1 = Array($input);
    $array2 = explode(',', $list);
    $array3 = array_diff($array2, $array1);

    $output = implode(',', $array3);

    echo $output;
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
112233
  • 2,406
  • 3
  • 38
  • 88

2 Answers2

6

Use explode on the $input variable as well:

$input = 'Wed';
$list = 'Mon,Tue,Wed,Thrs,Fri,Sat,Sun';
$array1 = explode(',', $input);
$array2 = explode(',', $list);
$array3 = array_diff($array2, $array1);

$output = implode(',', $array3);

echo $output;
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
0

you can store your list in array or just can explode it if you dnt want

 function weekdays($day){
    $list= array('Mon','Tue','Wed','Thrs','Fri','Sat','Sun');
    if (($key = array_search($day, $list )) !== false) {
        unset($list[$key]);
        return $list;
       }
    }
Tariq Husain
  • 559
  • 5
  • 23