Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex split string at first line break

Tags:

regex

php

I would like to split a string at the first line break, instead of the first blank line

'/^(.*?)\r?\n\r?\n(.*)/s' (first blank line)

So for instance, if I have:

$str = '2099 test\nAre you sure you want to continue\n some other string here...';

match[1] = '2099 test'
match[2] = 'Are you sure you want to continue\n some other string here...'
like image 232
delimiter2 Avatar asked Mar 15 '11 14:03

delimiter2


People also ask

How do I split a string into a new line?

Split a string at a newline character. When the literal \n represents a newline character, convert it to an actual newline using the compose function. Then use splitlines to split the string at the newline character. Create a string in which two lines of text are separated by \n .

How do you split a string only on the first instance of specified character?

To split a JavaScript string only on the first occurrence of a character, call the slice() method on the string, passing it the index of the character + 1 as a parameter. The slice method will return the portion of the string after the first occurrence of the character.

How do you split a line break?

To split a string by newline, call the split() method passing it the following regular expression as parameter - /\r?\ n/ . The split method will split the string on each occurrence of a newline character and return an array containing the substrings. Copied!


5 Answers

preg_split() has a limit parameter you can take to your advantage. You could just simply do:

$lines = preg_split('/\r\n|\r|\n/', $str, 2);
like image 140
Riimu Avatar answered Sep 30 '22 10:09

Riimu


<?php
$str = "2099 test\nAre you sure you want to continue\n some other string here...";

$match = explode("\n",$str, 2);
print_r($match);


?>

returns

Array
(
    [0] => 2099 test
    [1] => Are you sure you want to continue
 some other string here...
)

explode's last parameter is the number of elements you want to split the string into.

like image 35
Mike Lewis Avatar answered Sep 30 '22 10:09

Mike Lewis


Normally just remove on \r?\n:

'/^(.*?)\r?\n(.*)/s'
like image 25
Akarun Avatar answered Sep 30 '22 10:09

Akarun


You can use preg_split as:

$arr = preg_split("/\r?\n/",$str,2);

See it on Ideone

like image 24
codaddict Avatar answered Sep 30 '22 10:09

codaddict


First line break:

$match = preg_split('/\R/', $str, 2);

First blank line:

$match = preg_split('/\R\R/', $str, 2);

Handles all the various ways of doing line breaks.

Also there was a question about splitting on the 2nd line break. Here is my implementation (maybe not most efficient... also note it replaces some line breaks with PHP_EOL)

function split_at_nth_line_break($str, $n = 1) {
    $match = preg_split('/\R/', $str, $n+1);
    if (count($match) === $n+1) {
        $rest = array_pop($match);
    }
    $match = array(implode(PHP_EOL, $match));
    if (isset($rest)) {
        $match[] = $rest;
    }
    return $match;
}

$match = split_at_nth_line_break($str, 2);
like image 39
user9645 Avatar answered Sep 30 '22 10:09

user9645