How to split a string by multiple delimiters in PHP?


Question

"something here ; and there, oh,that's all!"

I want to split it by ; and ,

so after processing should get:

something here

and there

oh

that's all!
1
25
1/28/2014 3:18:48 PM

Accepted Answer

<?php

$pattern = '/[;,]/';

$string = "something here ; and there, oh,that's all!";

echo '<pre>', print_r( preg_split( $pattern, $string ), 1 ), '</pre>';

Updated answer to an updated question:

<?php

$pattern = '/[\x{ff0c},]/u';

//$string = "something here ; and there, oh,that's all!";
$string = 'hei,nihao,a ';


echo '<pre>', print_r( preg_split( $pattern, $string ), 1 ), '</pre>';
36
9/21/2009 3:51:02 AM

$result_array = preg_split( "/[;,]/", $starting_string );

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon