【发布时间】:2022-08-07 09:44:13
【问题描述】:
我需要按数字和空格拆分字符串,但不确定正则表达式。我的代码是:
$array = preg_split(\'/[0-9].\\s/\', $content);
$content 的值是:
Weight 229.6104534866 g
Energy 374.79170898476 kcal
Total lipid (fat) 22.163422468932 g
Carbohydrate, by difference 13.641848209743 g
Sugars, total 4.3691034101428 g
Protein 29.256342349938 g
Sodium, Na 468.99386390008 mg
这给出了结果:
Array ( [0] => Weight 229.61045348 [1] => g
Energy 374.791708984 [2] => kcal
Total lipid (fat) 22.1634224689 [3] => g
Carbohydrate, by difference 13.6418482097 [4] => g
Sugars, total 4.36910341014 [5] => g
Protein 29.2563423499 [6] => g
Sodium, Na 468.993863900 [7] => mg
) 1
我需要将文本与数字分开,但不确定如何,这样:
[0] => Weight
[1] => 229.60145348
[2] => g
等等...
我还需要它来忽略标签所在的逗号、括号和空格。使用explode时,我发现“总脂质(脂肪)”不是将一个值分成3个值,不知道如何用正则表达式解决这个问题。
当使用 explode() 我得到:
[0] => Total
[1] => lipid
[2] => (fat)
但我需要这些值作为标签的一个值,有什么方法可以忽略它吗?
非常感谢任何帮助!
-
为什么不使用
explode()函数? -
请您edit 包含minimal reproducible example - 向我们展示您打印的输出来自的输入,以及您想要该输入的确切输出。
标签: php arrays regex preg-split