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);
?>
Author by
michele
Updated on July 26, 2021Comments
-
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 over 12 yearsI tested. The link doesn't open well for me, but I'd assume it uses different flavor of regex.
-
Tom over 12 years@MichaelKrelin-hacker OP shouldn't use regexp in that case
-
Michael Krelin - hacker over 12 yearsI wouldn't use regex, you wouldn't. But it's up to OP :)
-
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 over 12 yearsTom, that's why I upvoted you, but you want to be not just right, but also impeccable.
-
Tom over 12 years@MichaelKrelin-hacker OP made me go FFFFUUUU, couldn't help it. Edited code's comments for impeccability.
-
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 over 12 years@michele, don't waste computing power, computers are people too.
-
Tom over 12 years@MichaelKrelin-hacker Now that's a shame. Going to meta right now. Wish I could upvote myself too.
-
michele over 12 yearsSo who consume less power? regexp or explode?
-
Tom over 12 yearsDefinetely explode ! Preg_split will bring your server's entire neighborhood down ! Talk about 2012 getting close.
-
michele over 12 yearsok, thanks but now i don't know who answer......each user take me a good and correct answer.
-
Liam W over 9 yearsThe issue is, what if it isn't using the \n newline character?
-
Bryan about 8 years@LiamW Exactly the problem I was having. See my answer
-
Abhi Beckert over 7 yearsThis does not answer the question. Not all strings use
\n
for newlines. -
Alex Skrypnyk almost 7 years
$lines = preg_split('~\R~',$content);
-
DesertEagle over 6 yearsit 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 almost 3 yearsThis does not split the rows apart. You are only solving half of the question.
-
mickmackusa almost 3 yearsThe 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 over 2 years'~\R~' will break Chinese character 先