PHP拆分和空间
当我用空格分割字符串时,它不会删除sapces。我的字符串如下所示:PHP拆分和空间,php,regex,Php,Regex,当我用空格分割字符串时,它不会删除sapces。我的字符串如下所示: $string = 'one - two - three'; list($blk1, $blk2, $blk3) = split('\s*\-\s*', $string); $string = 'one - two - three'; $parts = explode(' - ', $string); $blk1 = $parts[0]; $blk2 = $parts[1]; $blk3 = $parts[2]; 我的P
$string = 'one - two - three';
list($blk1, $blk2, $blk3) = split('\s*\-\s*', $string);
$string = 'one - two - three';
$parts = explode(' - ', $string);
$blk1 = $parts[0];
$blk2 = $parts[1];
$blk3 = $parts[2];
我的PHP调用如下所示:
$string = 'one - two - three';
list($blk1, $blk2, $blk3) = split('\s*\-\s*', $string);
$string = 'one - two - three';
$parts = explode(' - ', $string);
$blk1 = $parts[0];
$blk2 = $parts[1];
$blk3 = $parts[2];
我假设正则表达式将在零个或多个空间上拆分,但这些空间仍保留在结果数组中
知道为什么吗?您可以尝试以下方法:
[\s-]+
虽然不是100%熟悉php,但它应该可以工作。我不知道该如何回答直接问题,但正如您在手册页上看到的,此函数已被弃用,您不应该依赖它 为什么不这样使用explode:
$string = 'one - two - three';
list($blk1, $blk2, $blk3) = split('\s*\-\s*', $string);
$string = 'one - two - three';
$parts = explode(' - ', $string);
$blk1 = $parts[0];
$blk2 = $parts[1];
$blk3 = $parts[2];
在PHP中很久以前就被弃用了。考虑使用
为什么不使用explode()
这样就可以了:就像是一个针对正则表达式的计数器,用于这种带有分隔字符串的琐碎任务:
$string = 'one - two - three';
$parts = explode(' - ', $string);
array_walk($parts, 'trim');
var_dump($parts);
不使用拆分:(->使用预拆分