Pol*_*ino 1 php regex preg-replace preg-split
我如何摆脱preg_split结果中的尾随空格而不使用preg_replace先从$test字符串中删除所有空格?
$test = 'One , Two, Thee ';
$test = preg_replace('/\s+/', ' ', $test);
$pieces = preg_split("/[,]/", $test);
Run Code Online (Sandbox Code Playgroud)
如果必须preg_split()(你实际上在问题中要求)那么这可能会有所帮助:
$test = 'One , Two, Thee ';
$pieces = preg_split("/\s*,\s*/", trim($test), -1, PREG_SPLIT_NO_EMPTY);
Run Code Online (Sandbox Code Playgroud)
trim()用于删除第一个元素之前和最后一个元素之后的空格.(preg_split()没有 - 它只删除逗号周围的空格)
| 归档时间: |
|
| 查看次数: |
2898 次 |
| 最近记录: |