michele michele - 7 months ago 50
PHP Question

regular expression preg_split by 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 have returned in one array each row.
How can I do the regular expression?

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


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

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


Here the text were I am trying to do it http://regexr.com?2v23c .
Thanks a lot.

Tom Tom
Answer

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);
?>