开发者

Select from position to end of line in string

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开发者_JAVA百科 can't use this:

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

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


It should be :

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


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.


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.


If you need to process each individual line in the string you can always split the string into a array. You can do this using preg_split() in a way that will identify more than systems end of line character.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜