2017-07-25 110 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