views:

35

answers:

1

I want to explode a string for all:

  1. whitespaces (\n \t etc)
  2. comma
  3. hyphen (small dash). Like this >> -

But this does not work:

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

How to do that?

+6  A: 

Explode can't do that. There is a nice function called preg_split for that. Do it like this:

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

This outputs:

  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, do not use split, it is deprecated.

shamittomar
This worked fine.
WhatIsOpenID
So accept it as the answer ;)
pyrony
@pyrony, I just did it.
WhatIsOpenID