Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select from position to end of line in string

Tags:

string

php

Let's say I've got a string "blbl\nblbl etc". I know I should start at position $pos. I've got to select everything to the end of the line. I can't use this:

substr($string, $pos, strpos($string, "\n", $pos))

Mac doesn't use \n as a delimiter. What should I do?

like image 960
Morousej Avatar asked Mar 16 '11 22:03

Morousej


3 Answers

It should be :

substr($string, $pos, ( strpos($string, PHP_EOL, $pos) ) - $pos);
like image 198
Farhad Sakhaei Avatar answered Oct 08 '22 11:10

Farhad Sakhaei


You can try the PHP_EOL constant:

substr($string, $pos, strpos($string, PHP_EOL, $pos));

It contains the end-of-line character sequence of the OS the script is running.

like image 36
Felix Kling Avatar answered Oct 08 '22 11:10

Felix Kling


Pass the string through a function which replaces \r\n or \r with \n first. It's worth doing as a matter of course, unless you actually want to preserve platform specific line endings.

FWIW Mac's use \n now anyway.

like image 21
Rob Agar Avatar answered Oct 08 '22 12:10

Rob Agar