joink Posted April 14, 2010 Share Posted April 14, 2010 i am trying to create an array of from an external file based on the text on each line... this portion is done -- thanks to alot of help from you folks here. However Im having difficulty telling my code to ignore the first 33 lines of the file before trying to explode into variables... i thought i read somewhere implode may be a way of doing this but ive not found a working example. Any help? Quote Link to comment https://forums.phpfreaks.com/topic/198468-ignoreskip-first-33-lines-of-file/ Share on other sites More sharing options...
yozyk Posted April 14, 2010 Share Posted April 14, 2010 Can you show your code? Quote Link to comment https://forums.phpfreaks.com/topic/198468-ignoreskip-first-33-lines-of-file/#findComment-1041428 Share on other sites More sharing options...
joink Posted April 14, 2010 Author Share Posted April 14, 2010 the file i am grabbing the data from is in html format, i need to remove the hidden lines of code... meta tags etc.... with the code below it is trying to explode from line one, where the printed text that I need actually begins on line 34. and to note the subtr($content, 34) below only removes the "last updated" time stamp on this file. <?php $file = file("networkmap.asp.htm",FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES); array_shift($file); foreach($file as $value) { $content = strip_tags($value); //$content=trim($content); //Subtract Date $nodate_content = substr($content, 34); //subtract date/time list($router,$metric,$ping,$status)=explode(",",$nodate_content); $status=trim($status); $img = ($status == 'UP') ? '<img src="green.png" alt="up" />' : '<img src="red.png" alt="down" />'; echo "$router $img<hr />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/198468-ignoreskip-first-33-lines-of-file/#findComment-1041438 Share on other sites More sharing options...
Ken2k7 Posted April 14, 2010 Share Posted April 14, 2010 So you want to remove the first 33 entries in the $file array. Is that right? Quote Link to comment https://forums.phpfreaks.com/topic/198468-ignoreskip-first-33-lines-of-file/#findComment-1041440 Share on other sites More sharing options...
joink Posted April 14, 2010 Author Share Posted April 14, 2010 suppose thats the right wording, yep. right now I know that I am receiving all sorts of: Notice: Undefined offset: 3 in I:\webs\map3.php on line 11 Notice: Undefined offset: 2 in I:\webs\map3.php on line 11 Notice: Undefined offset: 1 in I:\webs\map3.php on line 11 for each of these lines of meta data/javascript etc that comes before the (now stripped) text. I was unable to get strip_tags to remove alot of tags - which may be because its ASP and not strictly html.. but in the end I just need to somehow ignore/skip/remove those first 33 lines or entries as you have said. Thanks for your responses Quote Link to comment https://forums.phpfreaks.com/topic/198468-ignoreskip-first-33-lines-of-file/#findComment-1041447 Share on other sites More sharing options...
oni-kun Posted April 14, 2010 Share Posted April 14, 2010 It shouldn't be hard at all. $i = 0; while($i<32) { unset($file[$i]); $i++; } I'm using a while loop as you see, since it uses less memory than a for loop. Quote Link to comment https://forums.phpfreaks.com/topic/198468-ignoreskip-first-33-lines-of-file/#findComment-1041450 Share on other sites More sharing options...
Ken2k7 Posted April 14, 2010 Share Posted April 14, 2010 Or just start looping at index 33 so you don't have to waste the time to unset them all. Quote Link to comment https://forums.phpfreaks.com/topic/198468-ignoreskip-first-33-lines-of-file/#findComment-1041451 Share on other sites More sharing options...
oni-kun Posted April 14, 2010 Share Posted April 14, 2010 Or just start looping at index 33 so you don't have to waste the time to unset them all. Notice his using of FOREACH, A FOR loop with an iterator would consume much more memory per iteration. Quote Link to comment https://forums.phpfreaks.com/topic/198468-ignoreskip-first-33-lines-of-file/#findComment-1041452 Share on other sites More sharing options...
Re321 Posted April 14, 2010 Share Posted April 14, 2010 I recommend oni-kun's option, It's pointless to recount arrays although the OP should really not be putting loops within loops (foreach->list) Quote Link to comment https://forums.phpfreaks.com/topic/198468-ignoreskip-first-33-lines-of-file/#findComment-1041456 Share on other sites More sharing options...
ignace Posted April 14, 2010 Share Posted April 14, 2010 Or just start looping at index 33 so you don't have to waste the time to unset them all. Notice his using of FOREACH, A FOR loop with an iterator would consume much more memory per iteration. This is just speculation if all those statistics have shown me one thing it is that it varies from case-to-case and php-version-to-php-version. Just use a for-loop starting at index 33 and runs up. Even if oni-kun's advice would apply in this case you would spare a few nano-seconds. Or make it a sure thing and benchmark it under various conditions to verify which solution actually eats the least amount of memory. Remember that you will need to perform this again whenever you modify your php settings Quote Link to comment https://forums.phpfreaks.com/topic/198468-ignoreskip-first-33-lines-of-file/#findComment-1041499 Share on other sites More sharing options...
salathe Posted April 14, 2010 Share Posted April 14, 2010 Another way would be to use the SPL's SplFileObject to be able to read the file easily, and a LimitIterator to start on line 34. If you're open to learning an entirely new approach (well, it's similar to the more usual fopen/fgets combo) then an example would look like: $file = new SplFileObject('networkmap.asp.htm'); // like fopen but in an OOP style $file->setFlags(SplFileObject::SKIP_EMPTY|SplFileObject::DROP_NEW_LINE); // flags like those used in fopen $limited = new LimitIterator($file, 33); // Start from 34th line (zero-based offset is 33) foreach ($limited as $value) { // $value is the same as in your original foreach } Quote Link to comment https://forums.phpfreaks.com/topic/198468-ignoreskip-first-33-lines-of-file/#findComment-1041504 Share on other sites More sharing options...
salathe Posted April 14, 2010 Share Posted April 14, 2010 I can't edit my post, so please note that in my code snippet any reference to "fopen" should be "file". Quote Link to comment https://forums.phpfreaks.com/topic/198468-ignoreskip-first-33-lines-of-file/#findComment-1041509 Share on other sites More sharing options...
joink Posted April 14, 2010 Author Share Posted April 14, 2010 I tried both approaches... oni-kun: lame I know, Im not sure where to place that code to make it work. Ive tried a few places and it hasnt changed my results ;( salathe: I am getting Fatal error: Undefined class constant 'SKIP_EMPTY' in I:\webs\map3.php on line 3 and cannot figure out why. I have found no reason why this should be erroring. PS im on PHP 5.1.1 Quote Link to comment https://forums.phpfreaks.com/topic/198468-ignoreskip-first-33-lines-of-file/#findComment-1041641 Share on other sites More sharing options...
salathe Posted April 14, 2010 Share Posted April 14, 2010 I have found no reason why this should be erroring. PS im on PHP 5.1.1 That flag (SplFileObject::SKIP_EMPTY) was only added in PHP 5.2.0 so you cannot use it with your 5.1.1 I'm afraid. Seems the documentation needs to note when this (and a couple others) were first introduced. Quote Link to comment https://forums.phpfreaks.com/topic/198468-ignoreskip-first-33-lines-of-file/#findComment-1041660 Share on other sites More sharing options...
Daniel0 Posted April 14, 2010 Share Posted April 14, 2010 I have found no reason why this should be erroring. PS im on PHP 5.1.1 That flag (SplFileObject::SKIP_EMPTY) was only added in PHP 5.2.0 so you cannot use it with your 5.1.1 I'm afraid. Seems the documentation needs to note when this (and a couple others) were first introduced. So get to it then Quote Link to comment https://forums.phpfreaks.com/topic/198468-ignoreskip-first-33-lines-of-file/#findComment-1041661 Share on other sites More sharing options...
joink Posted April 14, 2010 Author Share Posted April 14, 2010 with that being the case, i may try to upgrade my PHP - though I will have to request that be done for m. So, in the time being how do I use the method provided by oni-kun? im not sure where at in my code to place that suggestion ;\ Quote Link to comment https://forums.phpfreaks.com/topic/198468-ignoreskip-first-33-lines-of-file/#findComment-1041677 Share on other sites More sharing options...
joink Posted April 14, 2010 Author Share Posted April 14, 2010 actually whats happening is I need to remove the text on this file between the <script></script> tags.. which is what these 33 lines of text are. Once I strip_tags($value) it removes the script tags themselves but leaves the text between -- where the following code tries to explode it into an array. How can I approach removing that text while still exploding the data? I realize I need to do this before I strip away the <script></script> otherwise it wont locate anything to remove.. I tried: $cleaned = preg_replace("/(<script.*>.*<\/script>)/i","",$file); but that didnt seem to help.. is it because it may be looking (due to the foreach) for the <script></script> on each line? Quote Link to comment https://forums.phpfreaks.com/topic/198468-ignoreskip-first-33-lines-of-file/#findComment-1041797 Share on other sites More sharing options...
Daniel0 Posted April 14, 2010 Share Posted April 14, 2010 If you want to do that you'll have to use something like file_get_contents. You'll need to make your quantifiers lazy though. Otherwise you'll get problems with something like this: <script>foo</script> don't remove this <script>bar</script> Quote Link to comment https://forums.phpfreaks.com/topic/198468-ignoreskip-first-33-lines-of-file/#findComment-1041803 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.