For example, here is my string :
$text = "Iphone 4, Iphone 4S; Iphone 5, Iphone 3. Iphone 3S";
And splitting string:
$splitting_strings = array(".", ";", ".");
$result = array (
0 => "Iphone 4",
1 => "Iphone 4S",
2 => "Iphone 5",
3 => "Iphone 3",
4 => "Iphone 3S"
);
I am using this code:
$regex = '/(' . implode('|', $splitting_strings) . ')/';
print_r(implode($regex, $text));
You can using preg_split
$text = "Iphone 4, Iphone 4S; Iphone 5, Iphone 3. Iphone 3S";
$array = preg_split("/[\s]*[,][\s]*/", $text);
print_r($array);
// Array ( [0] => Iphone 4 [1] => Iphone 4S [2] => Iphone 5 [3] => Iphone 3 [4] => Iphone 3S )
EDIT:
$array = preg_split("/[\s]*[,]|[;]|[.][\s]*/", $text);
<?php
$text = "Iphone 4, Iphone 4S; Iphone 5, Iphone 3. Iphone 3S";
$splitting_strings = array_map( 'preg_quote', array('.', ';', '.', ',' ) );
$result = array_map( 'trim', preg_split( '~' . implode( '|', $splitting_strings ) . '~', $text ) );
The value of $result
is now the same as yours. Mind that I've used both preg_quote (to escape the characters) as trim.
Just to show an alternative to using regexp (though a regexp solution is more efficient).
$text = "Iphone 4, Iphone 4S; Iphone 5, Iphone 3. Iphone 3S";
$separators = ',;.';
$word = strtok($text, $separators);
$arr = array();
do {
$arr[] = $word;
$word = strtok($separators);
} while (FALSE !== $word);
var_dump($arr);