2017-07-25 70 views
0

当使用preg_split分割字符串时,下面的代码不保留分隔符。分割字符串时保留分隔符

$feature_description = "- 1.Read/Write speed performance is based on internal testing.- 2.TBW (terabytes written) values calculated capacity."; 


preg_split('/(- [0-9].)/',$feature_description,NULL,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 

现在的产量为:

[0] => - 1. 
    [1] => Read/Write speed performance is based on internal testing. 
    [2] => - 2. 
    [3] => TBW (terabytes written) values calculated capacity. 

但我想要的输出:

[1] => - 1.Read/Write speed performance is based on internal testing. 
    [2] => - 2.TBW (terabytes written) values calculated capacity. 

回答

1

而不是分裂你应该做使用这种超前基于正则表达式使用preg_match_all比赛:

-\h+\d+.+?(?=-\h+\d+|\z) 

RegEx Demo

正则表达式解体:

  • -\h+\d+:匹配连字符后面1+水平空格和数字1+
  • .+?:匹配零或多个任意字符(懒惰)
  • (?=-\h+\d+|\z) :前瞻性地断言我们有连字符,后跟1+水平空格和1+数字或字符串结尾
+0

它不是空的,这是直接链接:https://regex101.com/r/Dn0qLE/1 – anubhava

1

拆分与前瞻:

$feature_description = "- 1.Read/Write speed performance is based on internal testing.- 2.TBW (terabytes written) values calculated capacity."; 
$res=preg_split('/(?=- [0-9]+\.)/',$feature_description,NULL, PREG_SPLIT_NO_EMPTY); 
print_r($res); 

结果:

Array 
(
    [0] => - 1.Read/Write speed performance is based on internal testing. 
    [1] => - 2.TBW (terabytes written) values calculated capacity. 
) 

PHP demo

请注意,您不再需要PREG_SPLIT_DELIM_CAPTURE,因为正则表达式现在没有捕获组。

请注意,您需要转义点以匹配文字点。

图案的详细资料(?=- [0-9]+\.)是一个正向前查找该-之前相匹配的位置,一个空间,1个或多个数字,.

您可以提高正则表达式有点像

'/\s*(?=-\h[0-9]+\.)/' 

以便除去匹配(\s*)之间的任何空白和匹配-和一个数字之间的任何水平空白。

0

为什么你不能做到这一点:

$feature_description = "- 1.Read/Write speed performance is based on internal testing.- 2.TBW (terabytes written) values calculated capacity."; 

$feature_description_array = explode("-", $feature_description);//now we have an array broken up by the "-" delmiter 

现在你应该有类似如下的数组:

Array 
(
    [0] => 1.Read/Write speed performance is based on internal testing. 
    [1] => 2.TBW (terabytes written) values calculated capacity. 
) 

打印时你可能只是在前面加上失踪“ - ”与

echo "- ". Array[0]; 
+0

可能是因为字符串t本身可能包含分隔符“-'。这会破坏结果。正则表达式更可靠地服务这种情况。 – mickmackusa