Regular expression for preg_split () on a new line

This is my file:

0.0 5.0 5.0 6.0 7.0 2.0 5.0 2.0 1.0 5.0 5.0 1.0 2.0 7.1 5.0 5.0 0.0 5.0 5.0 5.0 2.0 5.0 1.0 5.0 6.0 6.0 6.0 6.0 1.0 7.1 5.0 5.0 0.0 6.0 1.0 6.0 5.0 5.0 1.0 6.0 5.0 7.0 1.0 5.0 6.0 6.0 5.0 6.0 0.0 5.0 2.0 1.0 6.0 5.0 6.0 2.0 1.0 2.0 1.0 5.0 7.0 5.0 1.0 5.0 0.0 7.0 1.0 1.0 2.0 1.0 5.0 6.0 2.0 2.0 5.0 2.0 2.0 6.0 2.0 7.0 0.0 5.0 5.0 6.0 5.0 2.0 5.0 1.0 2.0 5.0 5.0 5.0 5.0 1.0 1.0 5.0 0.0 2.0 6.0 1.0 5.0 7.0 5.0 1.0 6.0 7.0 1.0 5.0 1.0 2.0 2.0 1.0 5.0 6.0 5.0 2.0 6.0 7.0 0.0 5.0 5.0 7.0 6.0 5.0 5.0 5.0 6.0 2.0 6.0 2.0 1.0 5.0 6.0 5.0 0.0 

I would split it by \ n and return each row in a single array. How can I make a regex?

 $rows = preg_split('$regular_expression', $content); 

After I retrieve all the rows, how can I retrieve each value separated by backspace?

 $values_in_a_row = preg_split('$regular_expression', $a_row); 

Here is the text where I am trying to do this http://regexr.com?2v23c .

+9
source share
4 answers

No need for regular expressions:

 <?php $data = explode("\n", $data); // preg_split('#\n#', $data); Please don't foreach($data as &$row) { $row = explode(' ', $row); // preg_split('#\s#', $row); Seriously } print_r($data); ?> <test></test> 
+6
source

If you are having problems because you don’t know if each new line has only \n or \r\n or \r , then none of the above answers work and the regular expression works. What I did was

 $lines = preg_split("/(\r\n|\n|\r)/",$content); 

You can then use the accepted answer to separate the spaces.

+30
source
 $rowsapart = preg_split("/\n/",$rowstogether); $colspart = preg_split("/\s/",$colstogether); 
+4
source

There is no need for REGEX, use explode() instead:

 <?php $file = <<<EOF 0.0 5.0 5.0 6.0 7.0 2.0 5.0 2.0 1.0 5.0 5.0 1.0 2.0 7.1 5.0 5.0 0.0 5.0 5.0 5.0 2.0 5.0 1.0 5.0 6.0 6.0 6.0 6.0 1.0 7.1 5.0 5.0 0.0 6.0 1.0 6.0 5.0 5.0 1.0 6.0 5.0 7.0 1.0 5.0 6.0 6.0 5.0 6.0 0.0 5.0 2.0 1.0 6.0 5.0 6.0 2.0 1.0 2.0 1.0 5.0 7.0 5.0 1.0 5.0 0.0 7.0 1.0 1.0 2.0 1.0 5.0 6.0 2.0 2.0 5.0 2.0 2.0 6.0 2.0 7.0 0.0 5.0 5.0 6.0 5.0 2.0 5.0 1.0 2.0 5.0 5.0 5.0 5.0 1.0 1.0 5.0 0.0 2.0 6.0 1.0 5.0 7.0 5.0 1.0 6.0 7.0 1.0 5.0 1.0 2.0 2.0 1.0 5.0 6.0 5.0 2.0 6.0 7.0 0.0 5.0 5.0 7.0 6.0 5.0 5.0 5.0 6.0 2.0 6.0 2.0 1.0 5.0 6.0 5.0 0.0 EOF; $rows = explode("\n", $file); print_r($rows); echo "\n\n"; //Spacing $numbers_in_a_row = explode(" ", $rows[0]); print_r($numbers_in_a_row); ?> 

Living example

+2
source

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


All Articles