Jump to content

ignore/skip first 33 lines of file


joink

Recommended Posts

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?

Link to comment
Share on other sites

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 />";
}

?>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :P

Link to comment
Share on other sites

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 ;\

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.