开发者

how to count new lines in a very big string?

开发者 https://www.devze.com 2023-03-09 16:12 出处:网络
The problem reduces to counting \\n characters, so is there a function that can do it on a huge stri开发者_如何学Gongs, since explode() wastes too much memory.substr_count should do the trick:

The problem reduces to counting \n characters, so is there a function that can do it on a huge stri开发者_如何学Gongs, since explode() wastes too much memory.


substr_count should do the trick:

substr_count( $your_string, "\n" );


i Think substr_count( $your_string, "\n" ); should be:

$numLine = substr_count( $your_string, "\n" ) +1;

But I use this:

$numLine = count(explode("\n",$your_string));

it always return correct result


You can use PHP's substr_count() function: http://www.php.net/manual/en/function.substr-count.php

substr_count($myString, "\n");

It will give you an integer with the number of occurrences.


$count=preg_match_all ('/\n/',$str);
0

精彩评论

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