【问题标题】:How not to explode sub-delimiter inside delimeter?如何不爆炸分隔符内的子分隔符?
【发布时间】:2015-08-08 22:11:19
【问题描述】:

我有这个字符串:

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

我需要带有explode "("var_dump() 输出:

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

【问题讨论】:

  • 水果和技术怎么了?
  • @AbraCadaver 我猜他们只是被ᗧ(吃豆人)吃掉了。
  • @Rizier123: 哈哈 :-)
  • @user2337706:你为什么不使用“preg_split”?
  • 水果和技术可以留在这里,没那么重要。那么使用爆炸没有办法排除子分隔符?只有“preg_split”?

标签: php explode var-dump


【解决方案1】:

您可以使用带有正则表达式模式的 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" 
}

希望这会有所帮助。

【讨论】:

  • 谢谢你,你帮了我很多。
猜你喜欢
  • 1970-01-01
  • 2011-02-21
  • 1970-01-01
  • 1970-01-01
  • 2011-06-24
  • 1970-01-01
  • 2020-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多