Regular expression for preg_split() by new line

21,264

Solution 1

There isn't any 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>

Solution 2

If you are having issues because you don't know if each newline is just \n or \r\n or \r then none of the above answers work and a regexp works. What I did was

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

Then you can use the accepted answer to split the spaces.

Solution 3

$rowsapart = preg_split("/\n/",$rowstogether);

$colspart = preg_split("/\s/",$colstogether);

Solution 4

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

?>

Live Example

Share:
21,264
michele
Author by

michele

Updated on July 26, 2021

Comments

  • michele
    michele almost 3 years

    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 .

  • Michael Krelin - hacker
    Michael Krelin - hacker over 12 years
    I tested. The link doesn't open well for me, but I'd assume it uses different flavor of regex.
  • Tom
    Tom over 12 years
    @MichaelKrelin-hacker OP shouldn't use regexp in that case
  • Michael Krelin - hacker
    Michael Krelin - hacker over 12 years
    I wouldn't use regex, you wouldn't. But it's up to OP :)
  • Tom
    Tom over 12 years
    @MichaelKrelin-hacker It's up to me to tell the OP what I think of his use of regexp and make this world a better place.
  • Michael Krelin - hacker
    Michael Krelin - hacker over 12 years
    Tom, that's why I upvoted you, but you want to be not just right, but also impeccable.
  • Tom
    Tom over 12 years
    @MichaelKrelin-hacker OP made me go FFFFUUUU, couldn't help it. Edited code's comments for impeccability.
  • Tom
    Tom over 12 years
    @michele you don't need a regexp for a single character mask. The explode function is your friend in your case.
  • Michael Krelin - hacker
    Michael Krelin - hacker over 12 years
    @michele, don't waste computing power, computers are people too.
  • Tom
    Tom over 12 years
    @MichaelKrelin-hacker Now that's a shame. Going to meta right now. Wish I could upvote myself too.
  • michele
    michele over 12 years
    So who consume less power? regexp or explode?
  • Tom
    Tom over 12 years
    Definetely explode ! Preg_split will bring your server's entire neighborhood down ! Talk about 2012 getting close.
  • michele
    michele over 12 years
    ok, thanks but now i don't know who answer......each user take me a good and correct answer.
  • Liam W
    Liam W over 9 years
    The issue is, what if it isn't using the \n newline character?
  • Bryan
    Bryan about 8 years
    @LiamW Exactly the problem I was having. See my answer
  • Abhi Beckert
    Abhi Beckert over 7 years
    This does not answer the question. Not all strings use \n for newlines.
  • Alex Skrypnyk
    Alex Skrypnyk almost 7 years
    $lines = preg_split('~\R~',$content);
  • DesertEagle
    DesertEagle over 6 years
    it may be overpowered to use a regex, but it is not wrong. your answer might be working in this special case, but i voted down because of three things: 1) it is not even partially an answer to OPs question, 2) you did not explain WHY regular expressions are not the best idea in this case and 3) you didn't even bother to think about the feedback you already got and cared to edit your answer
  • mickmackusa
    mickmackusa almost 3 years
    This does not split the rows apart. You are only solving half of the question.
  • mickmackusa
    mickmackusa almost 3 years
    The problem with this code-only answer is that the OP and researchers may not understand how to set up this technique. In other words, they might not know how to declare $rowstogether and $colstogether.
  • John Wong
    John Wong over 2 years
    '~\R~' will break Chinese character 先