我下面的数据为纯文本格式,我正在尝试以特定格式对其进行排序,以便可以将每个值保存在数据库表中
G28585 alphabounce + 20 $ 55.00 $ 55.00 $ 1,100.00 FTWWHT / CYBEMT / ECRTIN尺寸9-11 11- 12 12-13 14数量2 6 2 2 1 4 3 D97028 adizero 8.0 100 $ 66.00 $ 66.00 $ 6,600.00 FTWWHT / POWRED / ACTRED尺寸9 9- 10 10- 11 11- 12 12- 13 13-14数量5 5 10 8 15 10 17 9 14 4 3 D97031 adizero 8.0 SK 68 $ 68.75 $ 68.75 $ 4,675.00 FTWWHT / POWRED / ACTRED尺寸10 10-11 11-12 12- 13 13- 14 15数量3 4 4 3 5 3 25 4 15 2 F97396碳含量低37 $ 49.50 $ 49.50 $ 1,831.50 FTWWHT / POWRED / ACTRED尺寸9 9-10 10-11 11-12-12-13 14数量2 1 3 4 3 2 6 3 7 6
我正在尝试以这种格式保存它,而我无法区分的是,我应该分隔文本,因为我没有任何分隔符:
G28585 alphabounce + 20 $ 55.00 $ 55.00 $ 1,100.00 FTWWHT / CYBEMT / ECRTIN尺寸9-11 11-12 12-13 13 14数量2 6 2 2 1 4 3
............
D97028 adizero 8.0 100 $ 66.00 $ 66.00 $ 6,600.00 FTWWHT / POWRED / ACTRED尺寸9 9-10 10-11 11-12 12-13 13-14数量5 5 10 8 15 10 17 9 14 4 3
答案 0 :(得分:3)
如果您的代码仅包含字符后跟5位数字,则可以这样做
$str = "G28585 alphabounce+ 20 $55.00 $55.00 $1,100.00 FTWWHT/CYBEMT/ECRTIN Size 9- 11 11- 12 12- 13 14 Qty 2 6 2 2 1 4 3 D97028 adizero 8.0 100 $66.00 $66.00 $6,600.00 FTWWHT/POWRED/ACTRED Size 9 9- 10 10- 11 11- 12 12- 13 13- 14 Qty 5 5 10 8 15 10 17 9 14 4 3 D97031 adizero 8.0 SK 68 $68.75 $68.75 $4,675.00 FTWWHT/POWRED/ACTRED Size 10 10- 11 11- 12 12- 13 13- 14 15 Qty 3 4 4 3 5 3 25 4 15 2 F97396 Freak Carbon Low 37 $49.50 $49.50 $1,831.50 FTWWHT/POWRED/ACTRED Size 9 9- 10 10- 11 11- 12 12- 13 14 Qty 2 1 3 4 3 2 6 3 7 6";
$splitted = preg_split('/(?=[A-Z][0-9]){6}/',$str,-1,PREG_SPLIT_NO_EMPTY);
print_r($splitted);
http://sandbox.onlinephpfunctions.com/code/7ad702ae20c9884c8c2348052610ad979f43d876
答案 1 :(得分:2)
您可以在产品代码的前面放置一个分隔符(这似乎是一个独特的样式),然后在上面分开。在这里,我使用换行符作为分隔符:
<?php
$txt = "G28585 alphabounce+ 20 $55.00 $55.00 $1,100.00 FTWWHT/CYBEMT/ECRTIN Size 9- 11 11- 12 12- 13 14 Qty 2 6 2 2 1 4 3 D97028 adizero 8.0 100 $66.00 $66.00 $6,600.00 FTWWHT/POWRED/ACTRED Size 9 9- 10 10- 11 11- 12 12- 13 13- 14 Qty 5 5 10 8 15 10 17 9 14 4 3 D97031 adizero 8.0 SK 68 $68.75 $68.75 $4,675.00 FTWWHT/POWRED/ACTRED Size 10 10- 11 11- 12 12- 13 13- 14 15 Qty 3 4 4 3 5 3 25 4 15 2 F97396 Freak Carbon Low 37 $49.50 $49.50 $1,831.50 FTWWHT/POWRED/ACTRED Size 9 9- 10 10- 11 11- 12 12- 13 14 Qty 2 1 3 4 3 2 6 3 7 6";
var_export(
preg_split(
'/\R/',
trim(
preg_replace(
'/([A-Z][0-9]+)/',
"\n$1",
$txt)
)
)
);
输出:
array (
0 => 'G28585 alphabounce+ 20 $55.00 $55.00 $1,100.00 FTWWHT/CYBEMT/ECRTIN Size 9- 11 11- 12 12- 13 14 Qty 2 6 2 2 1 4 3 ',
1 => 'D97028 adizero 8.0 100 $66.00 $66.00 $6,600.00 FTWWHT/POWRED/ACTRED Size 9 9- 10 10- 11 11- 12 12- 13 13- 14 Qty 5 5 10 8 15 10 17 9 14 4 3 ',
2 => 'D97031 adizero 8.0 SK 68 $68.75 $68.75 $4,675.00 FTWWHT/POWRED/ACTRED Size 10 10- 11 11- 12 12- 13 13- 14 15 Qty 3 4 4 3 5 3 25 4 15 2 ',
3 => 'F97396 Freak Carbon Low 37 $49.50 $49.50 $1,831.50 FTWWHT/POWRED/ACTRED Size 9 9- 10 10- 11 11- 12 12- 13 14 Qty 2 1 3 4 3 2 6 3 7 6',
)
答案 2 :(得分:2)
另一个带有正则表达式的解决方案
$raw = 'G28585 SOUTH DAKOTA, U OF SDAKO GAMEMODE POL 20 $55.00 $55.00 $1,100.00 FTWWHT/CYBEMT/ECRTIN Size 9- 11 11- 12 12- 13 14 Qty 2 6 2 2 1 4 3 D97028 adizero 8.0 100 $66.00 $66.00 $6,600.00 FTWWHT/POWRED/ACTRED Size 9 9- 10 10- 11 11- 12 12- 13 13- 14 Qty 5 5 10 8 15 10 17 9 14 4 3 D97031 adizero 8.0 SK 68 $68.75 $68.75 $4,675.00 FTWWHT/POWRED/ACTRED Size 10 10- 11 11- 12 12- 13 13- 14 15 Qty 3 4 4 3 5 3 25 4 15 2 F97396 Freak Carbon Low 37 $49.50 $49.50 $1,831.50 FTWWHT/POWRED/ACTRED Size 9 9- 10 10- 11 11- 12 12- 13 14 Qty 2 1 3 4 3 2 6 3 7 6';
$pattern ='/(?<style>\w+) (?<descr>[a-zA-Z, 0-9.+]+) (?<prices>[$0-9., ]+) (?<color>\w+\/\w+\/\w+) Size (?<size>[0-9 -]+) Qty (?<quantity>[0-9 ]+)/';
preg_match_all($pattern, $raw, $matches);
$matches = array_diff_key($matches, range(0, 6));
print_r($matches);
输出:
Array
(
[style] => Array
(
[0] => G28585
[1] => D97028
[2] => D97031
[3] => F97396
)
[descr] => Array
(
[0] => SOUTH DAKOTA, U OF SDAKO GAMEMODE POL 20
[1] => adizero 8.0 100
[2] => adizero 8.0 SK 68
[3] => Freak Carbon Low 37
)
[prices] => Array
(
[0] => $55.00 $55.00 $1,100.00
[1] => $66.00 $66.00 $6,600.00
[2] => $68.75 $68.75 $4,675.00
[3] => $49.50 $49.50 $1,831.50
)
[color] => Array
(
[0] => FTWWHT/CYBEMT/ECRTIN
[1] => FTWWHT/POWRED/ACTRED
[2] => FTWWHT/POWRED/ACTRED
[3] => FTWWHT/POWRED/ACTRED
)
[size] => Array
(
[0] => 9- 11 11- 12 12- 13 14
[1] => 9 9- 10 10- 11 11- 12 12- 13 13- 14
[2] => 10 10- 11 11- 12 12- 13 13- 14 15
[3] => 9 9- 10 10- 11 11- 12 12- 13 14
)
[quantity] => Array
(
[0] => 2 6 2 2 1 4 3
[1] => 5 5 10 8 15 10 17 9 14 4 3
[2] => 3 4 4 3 5 3 25 4 15 2
[3] => 2 1 3 4 3 2 6 3 7 6
)
)