This is actually a kind of combination of two SO questions split string into words by using space a delimiter AND Split string by other strings that I faced but didn't find a solution.
Let's say the array of delimiters is
$splitby = array('dlmtr1','dlmtr2','dlmtr3',' ','dlmtr5','dlmtr6');
$text = ' dlmtr1This is the string dlmtr2dlmtr2TTTdlmtr5WWWWW ';
$textArr = ('This', 'is', 'the', 'string', 'TTT', 'WWWWW');
$delimiterArr = (' dlmtr1', ' ', ' ', ' ', 'dlmtr2dlmtr2', 'dlmtr5',' ');
With another words it is true that
$text == $delimiterArr[0] . $textArr[0] . $delimiterArr[1] . $textArr[1] . ... . $delimiterArr(count($delimiterArr));
P.S. As result, each item of $delimiterArr
contains at least one or several delimiters as you see.
The steps of the possible solution for pattern is :
$pattern = '/\s?'.implode($splitby, '\s?|\s?').'\s?/';
Then I get wrong results in any way I continue.
Update: here is what I've got close to the expected result but the problem is the delimiters are split but they should come together if they are together in the text**
$splitby = array('dlmtr1','dlmtr2','dlmtr3',' ','dlmtr5','dlmtr6');
$text = ' dlmtr1This is the string dlmtr2dlmtr2TTTdlmtr5WWWWW ';
$pattern = '/\s?'.implode($splitby, '\s?|\s?').'\s?/';
$result = preg_split($pattern, $text, -1, PREG_SPLIT_NO_EMPTY);
preg_match_all($pattern, $text, $matches);
print_r($result);
print_r($matches[0]);
Results:
Array
(
[0] => This
[1] => is
[2] => the
[3] => string
[4] => TTT
[5] => WWWWW
)
Array
(
[0] =>
[1] => dlmtr1 '[0] and [1] should come together
[2] =>
[3] =>
[4] =>
[5] =>
[6] => dlmtr2 '[6] and [7] should come together
[7] => dlmtr2
[8] => dlmtr5
[9] =>
)
Thank you.