2017-03-09 21 views
1

我想分開使用php的「 - 」之前和之後的單詞。我無法分開帶有空格的文本和「 - 」之後的文本。如何打破前後「 - 」使用PHP?

<?php 
$value="this | is : my , text - test , done > hello-hi"; 
$keywords = preg_split("/[,|:&>]+/", $value); 
print_r($keywords); 
?> 

回答米獲得: 陣列([0] =>將此[1] =>是[2] =>我的[3] =>文本 - 試驗[4] =>完成[5] = >你好嗨)

答案我想要會是這樣的: Array([0] => this [1] => [2] => my [3] => text [4] => test [ 5] =>完成[6] =>你好喜)

+3

'爆炸( ' - ',$值,2)'將串分離成後的 ' - ' 和之後。 –

+0

http://php.net/manual/en/function.explode.php – Dimi

+0

我沒有得到結果,因爲我需要使用爆炸後也。 @Pheagey –

回答

1

我想你可以做一個gruop並添加一個OR|)分割-之間的話。

([,|:&>]+|\s+-\s+)將拆分,|:&>或一個或多個空格後跟一個-,後跟一個或多個空格。這避免了像hello-hi這樣的字符串/文本分成兩個元素。

$value="this | is : my , text - test , done > hello-hi"; 
$keywords = preg_split("/([,|:&>]+|\s+-\s+)/", $value); 
print_r($keywords); 

輸出:

Array ([0] => this [1] => is [2] => my [3] => text [4] => test [5] => done [6] => hello-hi) 
+0

謝謝,它爲我工作。 –