用逗号和-(连字符)多个爆炸字符

2021/01/18 16:21 · php ·  · 0评论

我想explode为所有字符串:

  1. 空格(\ n \ t等)
  2. 逗号
  3. 连字符(小破折号)。像这样>>-

但这不起作用:

$keywords = explode("\n\t\r\a,-", "my string");

怎么做?

爆炸无法做到这一点。preg_split为此有一个很好的功能 像这样做:

$keywords = preg_split("/[\s,-]+/", "This-sign, is why we can't have nice things");
var_dump($keywords);

输出:

  array
  0 => string 'This' (length=4)
  1 => string 'sign' (length=4)
  2 => string 'is' (length=2)
  3 => string 'why' (length=3)
  4 => string 'we' (length=2)
  5 => string 'can't' (length=5)
  6 => string 'have' (length=4)
  7 => string 'nice' (length=4)
  8 => string 'things' (length=6)

顺便说一句,不要使用split,它已被弃用。

...或者,如果您不喜欢正则表达式,但仍然想爆炸东西,则可以爆炸用一个字符替换多个字符

$keywords = explode("-", str_replace(array("\n", "\t", "\r", "\a", ",", "-"), "-", 
  "my string\nIt contains text.\rAnd several\ntypes of new-lines.\tAnd tabs."));
var_dump($keywords);

这吹成:

array(6) {
  [0]=>
  string(9) "my string"
  [1]=>
  string(17) "It contains text."
  [2]=>
  string(11) "And several"
  [3]=>
  string(12) "types of new"
  [4]=>
  string(6) "lines."
  [5]=>
  string(9) "And tabs."
}
本文地址:http://php.askforanswer.com/yongdouhaohe-lianzifuduogebaozhazifu.html
文章标签: ,   ,   ,  
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

文件下载

老薛主机终身7折优惠码boke112

上一篇:
下一篇:

评论已关闭!