2010-09-09 55 views
18

我想explode所有的字符串:多爆炸字符连字符( - )

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

但是,这并不工作:

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

如何做到这一点?

回答

45

爆炸无法做到这一点。有一个很好的功能,称为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) 

BTW,不要使用split,它已经过时了。