'PHP: delete the first line of a text and return the rest
What's the best way to remove the very first line of a text string and then echo the rest in 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:
sed -i~ 1d target-file
Or:
tail -n +2 source-file > target-file
Any ideas?
Solution 1:[1]
In alternative to the other answers with either explode & implode or regular expressions, you can also use strpos() and substr():
function stripFirstLine($text) {
return substr($text, strpos($text, "\n") + 1);
}
echo stripFirstLine("First line.\nSecond line.\nThird line.");
Live example: http://codepad.org/IoonHXE7
Solution 2:[2]
explode() it on the line breaks into an array, shift() off the first line, and rejoin the rest.
$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()
echo substr($t, strpos($t, "\n") + 1);
Solution 3:[3]
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
Solution 4:[4]
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.
$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);
Solution 5:[5]
More flexible solution where you can remove num lines from a string str using a seperator and return the rest.
The default seperator is \n. If you want to use a different seperator use a third argument when calling striplines() function.
function striplines($str,$num,$seperator="\n"){
$arr = explode($seperator, $str);
for ($i=0;$i<$num;$i++) array_shift($arr);
return implode($seperator, $arr);
}
//Testcases to remove first two lines
// returns/prints only Third line
echo striplines("First line.\nSecond line.\nThird line.",2);
// returns/prints nothing
echo striplines("First line.\nSecond line.\n",2);
echo striplines("First line.\nSecond line.",2);
echo striplines("First line.\n",2);
echo striplines("First line.",2);
echo striplines("",2);
Solution 6:[6]
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
Solution 7:[7]
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;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Michael Berkowski |
| Solution 3 | paullb |
| Solution 4 | RichardW11 |
| Solution 5 | |
| Solution 6 | PatrikAkerstrand |
| Solution 7 | Dan Bray |
