【问题标题】:Use explode in a smarter way以更智能的方式使用爆炸
【发布时间】:2015-05-19 18:15:03
【问题描述】:

我有以下字符串:

$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
}

【问题讨论】:

    标签: php explode


    【解决方案1】:

    我会使用preg_split():

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

    (?&lt;!\\\\) 是一个回顾。所以, 前面没有\。需要使用 \\\ 来表示单个 \,因为它是一个转义字符。

    【讨论】:

    • 感谢它有效!但它显示了`\`。你能告诉我如何摆脱它吗?
    • 我对其进行了测试,但我只想摆脱(/是)后跟逗号的斜杠..
    猜你喜欢
    • 1970-01-01
    • 2022-01-18
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    相关资源
    最近更新 更多