PHP REGEX - text to array by preg_split when line breaks

Edition:

need help splitting an array

Array Example

:

array ( [0] => :some normal text :some long text here, and so on... sometimes i'm breaking down and... :some normal text :some normal text ) 

ok now using

 preg_split( '#\n(?!s)#' , $text ); 

I get

 [0] => Array ( [0] => some normal text [1] => some long text here, and so on... sometimes [2] => some normal text [3] => some normal text ) 

I want to get this:

 [0] => Array ( [0] => some normal text [1] => some long text here, and so on... sometimes i'm breaking down and... [2] => some normal text [3] => some normal text ) 

that Regex can get the whole line, and also split it into line breaks !?

+4
source share
5 answers

Here is an example that works even if you have a colon character embedded inside the line (but not at the beginning of the line):

 $input = ":some normal text :some long text here, and so on... sometimes i'm breaking: down and... :some normal text :some normal text"; $array = preg_split('/$\R?^:/m', $input); print_r($array); 

result:

 Array ( [0] => some normal text [1] => some long text here, and so on... sometimes i'm breaking: down and... [2] => some normal text [3] => some normal text ) 
+7
source

"line break" is not defined. Windows uses only CR + LF (\ r \ n), Linux LF (\ n), OSX CR โ€‹โ€‹(\ r).

There is a little-known special character \ R in preg_ * regular exceptions that matches all three:

 preg_match('/^\R$/', "\r\n"); // 1 
+21
source

file() reads a file in an array.

+2
source

If you divide the array by a character: ..

 print_r(preg_split('/:/', $input)); 
0
source
 $lines = explode("\n", $text); 
-2
source

Source: https://habr.com/ru/post/1308060/


All Articles