Separating string based on values โโin an array
I have a group of strings that I need to write to an array. The string needs to be split by "/", "," with "or" & "Unfortunately, the string may contain two strings that need to be split, so I cannot use split or explode.
For example, a string might say "first past / go beyond and then turn", so I'm trying to get an array that will return array ('first past', 'going beyond', 'then turn')
The code I use is
$ splittersArray = array ('/', ',', 'with', '&');
foreach ($ splittersArray as $ splitter) {
if (strpos ($ string, $ splitter)) {
$ splitString = split ($ splitter, $ string);
foreach ($ splitString as $ split) {
I cannot find a function in PHP that allows me to do this. Should I pass the string back to the top of the sequence and keep going through the "foreach" after the string has been split over and over again?
It doesn't seem very efficient. Any suggestions would be great. Thanks, Pete
a source to share
Use regex and preg_split .
In the case you mentioned, you get the split array with:
$splitString = preg_split('/(\/|\,| with |\&/)/', $string);
a source to share