在PHP中分割/分解字符串

问题描述 投票:-2回答:1

我需要分割或爆炸字符串。

K1123-Food, Apple, Z3456-Egg, Mushroom, M9902-Plant, Soil, Water, Q8876-Medicine, Car, Splitter

变成类似

K1123-Food, Apple
Z3456-Egg, Mushroom
M9902-Plant, Soil, Water
Q8876-Medicine, Car, Splitter

欢迎使用preg_splitexplode的想法。

提前感谢。

php string explode preg-split
1个回答
0
投票
这里是使用正则表达式为您提供的解决方案:

$s = 'K1123-Food, Apple, Z3456-Egg, Mushroom, M9902-Plant, Soil, Water, Q8876-Medicine, Car, Splitter'; $pattern = '/(\w\d+-\w+(, [^\d]*))(, |$)/'; $matches = []; preg_match_all($pattern, $s, $matches); print_r($matches[1]);

© www.soinside.com 2019 - 2024. All rights reserved.