Splitting a string on multiple separators in PHP

前端 未结 6 523
走了就别回头了
走了就别回头了 2021-01-25 03:50

I can split a string with a comma using preg_split, like

$words = preg_split(\'/[,]/\', $string);

How can I use a dot, a space and

6条回答
  •  一个人的身影
    2021-01-25 04:48

    Try this:

     ["foo", "bar", "baz", "boo", "bat"]
    

    The Pattern explained

    [] is a character class, a character class consists of multiple characters and matches to one of the characters which are inside the class

    . matches the . Character, this does not need to be escaped inside character classes. Though this needs to be escaped when not in a character class, because . means "match any character".

    \s matches whitespace

    ; to split on the semicolon, this needs not to be escaped, because it has not special meaning.

    The + at the end ensures that spaces after the split characters do not show up as matches

提交回复
热议问题