A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearDelete first four lines from the top in content stored in a variable - page 1
User InfoPosts
Delete first four lines from the top in content stored in a variable#1
I have a variable that needs the first four lines stripped out before being displayed:

Error Report Submission
From: First Last, email@example.com, 12345
Date: 2009-04-16 04:33:31 pm Eastern

The content to be output starts here and can go on for any number of lines.


I need to remove the (header( from this data before I display it as part of a (pending error reports( view.

posted date: 2009-04-16 15:49:00


Re: Delete first four lines from the top in content stored in a variable#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-16 15:49:01


Re: Delete first four lines from the top in content stored in a variable#3
Mmm. I am sure someone is going to come up with something nifty/shorter/nicer, but how about:$str = implode("\n", array_slice(explode("\n", $str), 4));If that is too unsightly, you can always abstract it away:function str_chop_lines($str, $lines = 4) { return implode("\n", array_slice(explode("\n", $str), $lines));}$str = str_chop_lines($str);EDIT: Thinking about it some more, I wouldn(t recommend using the str_chop_lines function unless you plan on doing this in many parts of your application. The original one-liner is clear enough, I think, and anyone stumbling upon str_chop_lines may not realize the default is 4 without going to the function definition.

posted date: 2009-04-16 15:52:00


Re: Delete first four lines from the top in content stored in a variable#4
Split the string into an array using split(rex), where rex matches two consecutive newlines, and then concatenate the entire array, except for the first element (which is the header).

posted date: 2009-04-16 15:53:00


Re: Delete first four lines from the top in content stored in a variable#5
Strpos helps out a lot: Here(s an example:// $myString = "blah blah \n \n \n etc \n \n blah blah";$len = strpos($myString, "\n\n"); $string = substr($myString, $len, strlen($myString) - $len);$string then contains the string after finding those two newlines in a row.

posted date: 2009-04-16 15:55:00


Re: Delete first four lines from the top in content stored in a variable#6
$content = preg_replace("/^(.*\n){4}/", "", $content);

posted date: 2009-04-16 15:56:00


Re: Delete first four lines from the top in content stored in a variable#7
Have you tested this? It it not working me, strpos returns the position of the first occurrence, not whatever comes after.

posted date: 2009-04-16 16:02:00


Re: Delete first four lines from the top in content stored in a variable#8
Sorry about that - edited my post to do it a different way and left out the part that actually did it!

posted date: 2009-04-16 16:03:00


Re: Delete first four lines from the top in content stored in a variable#9
Have you tested this? :) It's not working either. I'm not sure why, though.

posted date: 2009-04-16 16:05:00


Re: Delete first four lines from the top in content stored in a variable#10
Yeah - kanago.net/loltest.php and the same .phps has it. The commented out string actually has spaces between newlines unlike the actual string. Oopsies. Embarrass me enough yet? :D

posted date: 2009-04-16 16:12:00


Re: Delete first four lines from the top in content stored in a variable#11
Haha, just looking out. :) I wasn't testing it with the string you gave, though, it isn't working on my machine with the string the OP gave. Odd. Shrug

posted date: 2009-04-16 16:16:00


Re: Delete first four lines from the top in content stored in a variable#12
Yeah, at first I thought it was ugly but it's pretty clear on what it does, I think.

posted date: 2009-04-16 16:20:00


select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.