2015-08-08 58 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