以更智能的方式使用爆炸

时间:2015-05-19 18:15:03

标签: php explode

我有以下字符串:

$string = "This is my string, that I would like to explode. But not\, this last part";

我想explode(',', $string)字符串,但当逗号前面有explode()时,\不应该爆炸。

通缉结果:

array(2) {
  [0] => This is my string
  [1] => that I would like to explode. But not , this last part
}

1 个答案:

答案 0 :(得分:3)

我会使用preg_split()

$result = preg_split('/(?<!\\\),/', $string);

print_r($result);

(?<!\\\\)是一个外观。因此,前面没有\。需要使用\\\来表示单个\,因为它是转义字符。

相关问题