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 stri开发者_如何学运维ng 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?
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
How about 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
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);
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
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);
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);
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
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;
}
精彩评论