PHP:删除文本的第一行并返回其余部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7740405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: delete the first line of a text and return the rest
提问by Roger
What's the best way to remove the very first line of a text string and then echo the rest in PHP?
删除文本字符串的第一行然后在 PHP 中回显其余部分的最佳方法是什么?
For example.
例如。
This is the text string:
这是文本字符串:
$t=<<<EOF
First line to be removed
All the rest
Must remain
EOF;
This is the final output:
这是最终的输出:
All the rest
Must remain
If I was working with a file in Bash I could do easily the next:
如果我在 Bash 中处理文件,我可以轻松地执行以下操作:
sed -i~ 1d target-file
sed -i~ 1d target-file
Or:
或者:
tail -n +2 source-file > target-file
tail -n +2 source-file > target-file
Any ideas?
有任何想法吗?
回答by ComFreek
Instead of explode()
and implode
, you can also use strpos()
and substr()
.
取而代之的explode()
和implode
,你也可以使用strpos()
和substr()
。
<?php
function stripFirstLine($text)
{
return substr( $text, strpos($text, "\n")+1 );
}
echo stripFirstLine( "First line.\nSecond line.\nThird line.");
?>
This method is also faster than working with arrays. See my speed test: http://codepad.org/GTom43HJ
这种方法也比使用数组更快。看我的速度测试:http: //codepad.org/GTom43HJ
Live example: http://codepad.org/P8KXnqQf
现场示例:http: //codepad.org/P8KXnqQf
回答by matino
How about preg_replace:
preg_replace 怎么样:
$text = "First line.\nSecond line.\nThird line.";
echo preg_replace('/^.+\n/', '', $text);
This way you don't need to worry about the case where there is no newline in your file.
http://codepad.org/fYZuy4LS
这样您就不必担心文件中没有换行符的情况。
http://codepad.org/fYZuy4LS
回答by Michael Berkowski
explode()
it on the line breaks into an array, shift()
off the first line, and rejoin the rest.
explode()
它在第一行分成一个数组,shift()
从第一行开始,然后重新加入其余的。
$arr = explode("\n", $t);
array_shift($arr);
echo implode("\n", $arr);
// Prints
// All the rest
// Must remain
If your string is really large, this will use a lot of memory. But if your strings are comparable to your example, it will work fine.
如果您的字符串非常大,这将使用大量内存。但是,如果您的字符串与您的示例具有可比性,则它会正常工作。
Method 2, using strpos()
方法二,使用 strpos()
echo substr($t, strpos($t, "\n") + 1);
回答by paullb
I know it's a late answer, but why wouldn't you just use an explode and limit the number of results
我知道这是一个迟到的答案,但你为什么不直接使用爆炸并限制结果的数量
$parts = explode("\n", $test, 2);
//$parts[0] has the first line
//$parts[1] has everything else
回答by PatrikAkerstrand
Return a substring after the first newline-character:
在第一个换行符之后返回一个子字符串:
$firstLineRemoved = $subject;
$firstNewlinePosition = strpos($subject, "\n");
if($firstNewlinePosition !== false)
{
$firstLineRemoved = substr($subject, firstNewlinePosition +1);
}
echo $firstLineRemoved;
Edit: Same example as @ComFreek, but with error checking in case there is no new-line character
编辑:与@ComFreek 相同的示例,但在没有换行符的情况下进行错误检查
回答by Jan
More flexible solution where you can remove 1 or more lines and return the rest.
更灵活的解决方案,您可以删除 1 行或多行并返回其余行。
function striplines($s,$n){
$arr = explode("\n", $s);
for ($i=0;$i<$n;$i++) array_shift($arr);
return implode("\n", $arr);
}
//remove first two lines
echo striplines("First line.\nSecond line.\nThird line.",2); //returns/prints only Third line
回答by RichardW11
I tried all of these, and none seemed to work fast enough with large files (50MB+). Here was the solution I created. In your case, you could omit the echo of the first line.
我尝试了所有这些,但对于大文件(50MB+)似乎没有一个足够快。这是我创建的解决方案。在您的情况下,您可以省略第一行的回声。
$fh = fopen($local_file, 'rb');
echo "add\tfirst\tline\n"; // add your new first line.
fgets($fh); // moves the file pointer to the next line.
echo stream_get_contents($fh); // flushes the remaining file.
fclose($fh);
回答by Dan Bray
All the answers are inefficient. There is no need to use any functions to manipulate the string.
所有的答案都是低效的。不需要使用任何函数来操作字符串。
$pos = 0;
while ($str[$pos] !== PHP_EOL)
$str[$pos++] = '';
echo $str;
If you are not sure that the string always contains more than one line use this:
如果您不确定字符串总是包含多行,请使用以下命令:
if (strpos($str, PHP_EOL))
{
$pos = 0;
while ($str[$pos] !== PHP_EOL)
$str[$pos++] = '';
echo $str;
}