0

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.

Community
  • 1
  • 1
Haradzieniec
  • 8,150
  • 26
  • 100
  • 199

1 Answers1

1

Below code runs as expected.

$splitby = array('dlmtr1','dlmtr2','dlmtr3',' ','dlmtr5','dlmtr6');
$text = '  dlmtr1This is     the string dlmtr2dlmtr2TTTdlmtr5WWWWW ';

preg_match_all("/\s*(dlmtr[1-6])+\s*|\s+/", $text, $matches);
echo "<pre>";print_r($matches[0]);echo "</pre>";

Array
(
    [0] =>   dlmtr1
    [1] =>  
    [2] =>      
    [3] =>  
    [4] =>  dlmtr2dlmtr2
    [5] => dlmtr5
    [6] =>  
)

$result = explode(' ', trim(preg_replace("/\s*(dlmtr[0-9])+\s*|\s+/",' ', $text)));
echo "<pre>";print_r($result);echo "</pre>";

Array
(
    [0] => This
    [1] => is
    [2] => the
    [3] => string
    [4] => TTT
    [5] => WWWWW
)
Archi
  • 445
  • 3
  • 12