After you mentioned in comments that you have raw data in CSV format, the use of regular expression is not the best solution.
Instead split the data into an array with PHP's CSV functions, like str_getcsv:
$csv = 'F251,43%,"3,50 €","0,50 €","0,50 €",,"0,50 €","0,50 €","0,50 €","0,49 €",
"0,49 €",,"0,47 €",,"0,47 €",,"0,46 €","0,46 €","0,44 €","0,44 €","0,44 €",,
"0,43 €",,"0,43 €","0,43 €",,,"0,41 €",,,"0,40 €","0,40 €",,"0,39 €",
"0,39 €",,"0,37 €","0,37 €","0,36 €","0,36 €","0,36 €","0,36 €","0,36 €"';
$data = str_getcsv($csv);
var_export ($data);
Output:
array (
0 => 'F251',
1 => '43%',
2 => '3,50 €',
3 => '0,50 €',
4 => '0,50 €',
5 => '',
6 => '0,50 €',
7 => '0,50 €',
8 => '0,50 €',
9 => '0,49 €',
10 => '0,49 €',
11 => '',
12 => '0,47 €',
13 => '',
14 => '0,47 €',
15 => '',
16 => '0,46 €',
17 => '0,46 €',
18 => '0,44 €',
19 => '0,44 €',
20 => '0,44 €',
21 => '',
22 => '0,43 €',
23 => '',
24 => '0,43 €',
25 => '0,43 €',
26 => '',
27 => '',
28 => '0,41 €',
29 => '',
30 => '',
31 => '0,40 €',
32 => '0,40 €',
33 => '',
34 => '0,39 €',
35 => '0,39 €',
36 => '',
37 => '0,37 €',
38 => '0,37 €',
39 => '0,36 €',
40 => '0,36 €',
41 => '0,36 €',
42 => '0,36 €',
43 => '0,36 €',
)
If you would like to get rid of the €
and %
signs, and/or use the decimal point for numbers (to allow further calculations in PHP), then you could do this:
$data = array_map(function ($v) {
// get rid of `€` or `%` at the end of values:
$v = preg_replace('/( €|%)$/', '', $v);
// if you want to replace the decimal comma to point for further calculations:
$num = str_replace(',', '.', str_replace('.', '', $v));
return is_numeric($num) ? $num : $v;
}, $data);
var_export ($data);
Which outputs:
array (
0 => 'F251',
1 => '43',
2 => '3.50',
3 => '0.50',
4 => '0.50',
5 => '',
6 => '0.50',
7 => '0.50',
8 => '0.50',
9 => '0.49',
10 => '0.49',
11 => '',
12 => '0.47',
13 => '',
14 => '0.47',
15 => '',
16 => '0.46',
17 => '0.46',
18 => '0.44',
19 => '0.44',
20 => '0.44',
21 => '',
22 => '0.43',
23 => '',
24 => '0.43',
25 => '0.43',
26 => '',
27 => '',
28 => '0.41',
29 => '',
30 => '',
31 => '0.40',
32 => '0.40',
33 => '',
34 => '0.39',
35 => '0.39',
36 => '',
37 => '0.37',
38 => '0.37',
39 => '0.36',
40 => '0.36',
41 => '0.36',
42 => '0.36',
43 => '0.36',
)
Original answer based on regular expressions
You could do this with a regular expression, but it depends on what your rules are for splitting other strings. This regular expression assumes the string will always have the same number of items in the same order, and concerning the F010
and F020
it assumes that these always each occupy exactly 4 characters and are always present:
$string = "format,surcharge,amount,1,card,basicprice 3,50 F010F020,0%,3,50 ,,,";
preg_match("/(.*?),(.*?),(.*?),(.*?),(.*?),(.*?)\s+(.*?)\s+(.{4})(.{4}),(.*?),(\d+,\d\d)\s/",
$string, $matches);
var_export ($matches);
This outputs:
array (
0 => 'format,surcharge,amount,1,card,basicprice 3,50 F010F020,0%,3,50 ',
1 => 'format',
2 => 'surcharge',
3 => 'amount',
4 => '1',
5 => 'card',
6 => 'basicprice',
7 => '3,50',
8 => 'F010',
9 => 'F020',
10 => '0%',
11 => '3,50',
)