问题减少到计算\n字符,所以有一个函数可以在一个巨大的字符串上执行它,因为explode()浪费了太多的内存.
Geo*_*ins 88
substr_count应该做的伎俩:
substr_count( $your_string, "\n" );
Run Code Online (Sandbox Code Playgroud)
您可以使用PHP的substr_count()功能:http://www.php.net/manual/en/function.substr-count.php
substr_count($myString, "\n");
它会给出一个包含出现次数的整数.
我认为substr_count($ your_string,“ \ n”); 应该:
$numLine = substr_count( $your_string, "\n" ) +1;
Run Code Online (Sandbox Code Playgroud)
但是我用这个:
$numLine = count(explode("\n",$your_string));
Run Code Online (Sandbox Code Playgroud)
它总是返回正确的结果