Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split String by Multiple Delimiters in PHP

Tags:

php

Can strings be parsed into an array based on multiple delimiters? as explained in the code:

$str ="a,b c,d;e f";
//What i want is to convert this string into array
//using the delimiters space, comma, semicolon
like image 718
Shahid Karimi Avatar asked Mar 24 '11 04:03

Shahid Karimi


1 Answers

PHP

$str = "a,b c,d;e f";

$pieces = preg_split('/[, ;]/', $str);

var_dump($pieces);

CodePad.

Output

array(6) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
  [4]=>
  string(1) "e"
  [5]=>
  string(1) "f"
}
like image 68
alex Avatar answered Nov 14 '22 23:11

alex