I have a file with some data in the format of:
0101 Group1 01/13/13
0102 Group1 02/03/13
0103 Group1 03/05/13
0104 Group1 04/05/13
0201 Group2 04/19/13
0202 Group2 05/10/13
0301 Group3 07/13/13
0302 Group3 07/13/13
0303 Group3 07/13/13
0401 Group4 02/12/13
0501 Group5 05/29/13
I only have a total of 5 Groups. I am trying to replace each Group with a single letter value here.
Group1 will be replaced with A
Group2 will be replaced with B
Group3 will be replaced with C
Group4 will be replaced with D
Group5 will be replaced with E
I found a way to do this with preg_replace_callback.
$text = preg_replace_callback('/Group[1-5]/', 'id_callback', $text);
function id_callback($matches) {
if ($matches[0] == 'Group1') {
return 'A';
} elseif ($matches[0] == 'Group2') {
return 'B';
} elseif ($matches[0] == 'Group3') {
return 'C';
} elseif ($matches[0] == 'Group4') {
return 'D';
} elseif ($matches[0] == 'Group5') {
return 'E';
}
}
echo $text;
Is there a way I can do this and get rid of all these if statements?