Divide text into several texts

0

I need to separate a text into parts (array). Text similar to this:

  

Lorem ipsum pain sit amet, consectetur adipiscing elit. Donec eget   magna faucibus, fermentum nibh dapibus, dictum.

     

Integer eleifend lorem nec velit tincidunt, eu elementum nulla cursus.

     

[Separator1] Donec arcu quam, posuere non molestie eu, bibendum non   enim. [/ Separator1]

     

Phasellus ante nulla, euismod vitae magna porta, iaculis posuere   velit.

     

[Separator2] Mauris sed fermentum justo, at hendrerit sem.   [/ Separator2]

     

Cras dapibus magna vel urn facilisis, thirsty fringilla urn interdum. In   I will thirst nibh sed pharetra.

I've done it with preg_match_all in the following way:

$re = '/\[Separador1\](.*?)\[\/Separador1\]|\[Separador2\](.*?)\[\/Separador2\]/ms';
preg_match_all($re, $content, $matches, PREG_SET_ORDER, 0);

However the result contains only what is inside [Separator1] ... [/ Separator1] and [Separator2] ... [/ Separator2] but I need the result in array also to contain the text that is on top of it. [Separator1] and [Separator2]

    
asked by Tony 26.09.2018 в 13:41
source

1 answer

1

You can use the explode method of php . This function needs which is the separator for which you want to divide and the text you want to divide.

$txt = "Hola,Hello,Hi";
$partes = explode(",", $txt);
//$partes[0] = "Hola";
//$partes[1] = "Hello";
//$partes[2] = "Hi";

In your case you would need to divide by line break so you need to use the following:   explode("\n", $txt);

    
answered by 26.09.2018 в 13:51