Sam
Sam

Reputation: 21

PHP and regex to cut from begining to new line

I don't have any expirience in regex with php (just some copy/pasting for simple tasks) so I can't 'solve' simple situatio, and I hope that somone can help.

I have a lage files that look like:

xxxxxxxxxxxxxx
xxxxxxxxxxxxxxx
xxxxxxxxxxxxxx
text,text,text,
text,text,tec,etc

Now i need in php to write function where passed string with content like this will return me part that I marked here as 'xxx...' After every 'xxxx...' sequence at file begin, I have as you can see new line (\r\n) and I don't know how to use regex to to cut upper part into another string, so please help.

Upvotes: 1

Views: 178

Answers (2)

user229044
user229044

Reputation: 239382

If the only part of the string separated by two new lines is the header series of 'xxxx' and the following 'text,text,text' lines, you could simply use explode. Passing a limit of 2 means it will only explode at the first instance of "\n\n", returning an array of at most 2 items:

list($header, $body) = explode("\n\n", $file_contents, 2);

Upvotes: 1

user7675
user7675

Reputation:

I'd skip the regex.

$eol = strpos($s, "\r\n");
$header = substr($s, 0, $eol);
$rest = substr($s, $eol + 2); 

Upvotes: 1

Related Questions