2015-08-08 90 views
0

我有這個字符串:如何不爆炸分隔符內的子分隔符?

text example (some text) (usb, apple (fruit), computer (technology), nature: sky) 

我需要這個var_dump()輸出與explode "("

array(3) { 
    [0]=> 
    string(34) "text example" 
    [1]=> 
    string(12) "some text" 
    [2]=> 
    string(12) "usb, apple, computer, nature: sky" 
} 
+1

水果和科技發生了什麼變化? – AbraCadaver

+0

@AbraCadaver我猜他們只會被ᗧ(pacman)吃掉。 – Rizier123

+0

@ Rizier123:哈哈:-) – Eugen

回答

0

您可以使用PHP函數preg_replace()用正則表達式來刪除文本,你不希望呈現輸出後使用explode功能:

$string = 'text example (some text) (usb, apple (fruit), computer (technology), nature: sky)'; 

//Remove (fruit) and (technology) from string 
$newString = preg_replace('/ \((\w+)\)/i', ', ', $string); 

//Explode with new string 
$output = explode(" (",$newString); 

//Remove ')' from output 
var_dump(preg_replace('/\)/i', '', $output)); 

結果:

array(3) { 
    [0]=> string(12) "text example" 
    [1]=> string(9) "some text" 
    [2]=> string(35) "usb, apple, computer, nature: sky" 
} 

希望這會有所幫助。

+0

謝謝,你幫了我很多。 – kostya572