开发者

Select from position to end of line in string

开发者 https://www.devze.com 2023-02-17 12:22 出处:网络
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:

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

精彩评论

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

关注公众号