I have string as following:
$str = "Q1-Q4,A3-A6,S9-S11";
Can someone tell me how to split the above text in PHP so that I get following as output:
Q1,Q2,Q3,Q4,A3,A4,A5,A6,S9,S10,S11
I have string as following:
$str = "Q1-Q4,A3-A6,S9-S11";
Can someone tell me how to split the above text in PHP so that I get following as output:
Q1,Q2,Q3,Q4,A3,A4,A5,A6,S9,S10,S11
This should get you going. This separates the sequences, adds in extra items and returns all of this as an array you can conveniently use.
$str = "Q1-Q4,A3-A6,S9-S11";
$ranges = explode (',', $str);
$output = [];
foreach ($ranges as $range) {
$items = explode ('-', $range );
$arr = [];
$fillin = [];
$letter = preg_replace('/[^A-Z]/', '', $items[0]);
$first = preg_replace('/[^0-9]/', '', $items[0]);
$last = preg_replace('/[^0-9]/', '', end($items));
for($i = $first; $i-1 < $last; $i++) {
$fillin[] = $letter . $i;
}
$output[] = $fillin;
}
var_dump( $output );
exit;