Search code examples
phpsplitexplode

Is it possible to have 3 delimiters for the explode function


Option 1 (spaces)

keyword keyword keyword

Option 2 (line breaks)

keyword
keyword
keyword

Option 3 (commas)

keyword, keyword, keyword

Or would I have to use the split function instead? And if so, how?


Solution

  • Try using preg_split but notice that this will explode on all of your examples at once.

    $parts = preg_split("/[ ,\n]/", $string);
    

    Edit: For the third example you give you'll get empty array elements as it's being split on both the comma and the space. Pass $parts through array_filter() to strip these out.