php simple array question

alexho

New Member
Reaction score
0
i have a quick question about php functions for the specific purpose of loading files into an array. I do not use file() as it was greatly reduced in speed in newer vers i use fgets btw..
Question: calling functions to get a line like fgets goes quicker on lines that have less data?.. should be obvious or??
and another question to ponder
Question: it would be nice if there was a way to get lines that are not from the start of a file, but now it is impossible to for example load a text file like this:
Code:
apples
carrots
bananas
pears
where the pointer goes to array 2 bananas and downwards. i have yet to seen this to be possible, so i guess thats why im asking a question about it. it would be amazing if developers or an existing function exists to get a file into an array at midway line or whichever line and downwards.. :rolleyes:
 

enouwee

Non ex transverso sed deorsum
Reaction score
240
It doesn't exist, because it's straightforward to write. PHP is already bloated enough, it doesn't need a built-in function for anything its lazy developers dream of.

The brain-dead one-liner way:
PHP:
<?php

print_r(array_slice(file('test'), 1, 9));

?>

Or the I-am-able-to-write-it-the-more-efficient way:
PHP:
<?php

function fmid($filename, $start = 0, $end = -1)
{
  if ($start < 0)
  {
    return false;
  }

  if ($end >= 0)
  {
    if ($end < $start)
    {
      return false;
    }
    else
    {
      $end -= $start;
    }
  }

  $fh = fopen($filename, 'r');
  if (!is_resource($fh))
  {
    return false;
  }

  $rv = array();
  for($i = 0; $i < $start; $i++)
  {
    if (fgets($fh) === false)
    {
      fclose($fh);
      return false;
    }
  }

  if ($end < 0)
  {
    while(!feof($fh))
    {
      if (($line = fgets($fh)) === false)
      {
        if (!feof($fh))
        {
          $rv = false;
        }

        break;
      }
      $rv[] = $line;
    }
  }
  else
  {
    for ($i = 0; $i <= $end; $i++)
    {
      if (($line = fgets($fh)) === false)
      {
        if (!feof($fh))
        {
          $rv = false;
        }

        break;
      }
      $rv[] = $line;
    }
  }

  fclose($fh);

  return $rv;
}

?>
 

alexho

New Member
Reaction score
0
yes that would work 'array_slice' ive seen it before :banghead: idiot (me) thanks :rolleyes:
i would have to agree n2lbr with replace \n :) not sure which is quicker but however this would be just for slow a file slowly crept through not in fgets
 

alexho

New Member
Reaction score
0
what exactly does fgets limiter do, count bytes per line, to the specified #, then go to new line? obviously this is not true, what is the real 'purpose' of limit anyway, I cannot seem to find much info on it. in depth for stuff is there any place that has alot of info, didnt see much in php.net?


here something more simple i am trying to accomplish, i guess i didnt explain well enough. below is obviously a cookie cutter, but works but since im looking for true efficiency it is dumb because it would keep grabbing from the file never the less it just waits till the number is right. a method of not counting it but starting at start seems hard i would just need to get fgets to return false continuously eh
Code:
function astart($filename,$start) {
$x=0;
$xx = fopen ($filename, 'r');
while (!feof ($xx)) {
if ($x<$start && $x>=0) {
$buffer = fgets($xx);
echo "$x, $buffer :";
unset($buffer);
// '<script>alert("x is '.$x.' less than start, '.$buffer.'");</script>';

}else{
$buffer = fgets($xx, 4096);
}
      if ($x>=$start) {   

   if ($buffer!="" || !empty($buffer)) {
   //$faa[] = $buffer;
   $startfromstart.=$buffer;
   }
      }
   $x++;
}
fclose ($xx);

return $startfromstart;

}
for example i have lines that have a constant of NO more than 12 each line
i would do fgets(file, 13); since its -1 then it goes to the next line? i have read it and tried it but it seems not to work. there is some overseen flaw i dont see.
TO most simply 'say' this if i have very small lines that are hardly anything in size but at a certain length no more wouldnt it be better to set a smaller value? and what are those best values to use for smaller horizontal lines?
 

enouwee

Non ex transverso sed deorsum
Reaction score
240
It returns a zero-terminated string of up to n-1 bytes unless a newline is encountered.

Example: you have a file with the following content: "0123456789abcdefghijABC"

Code:
fgets($the_file, 11);
-> "0123456789"
fgets($the_file, 11);
-> "abcdefghij"
fgets($the_file, 11);
-> "ABC"

If you want the whole line for sure, just omit the length argument.
 

alexho

New Member
Reaction score
0
ok thanks for that then, it grabs the different characters. i will just use no length
if need the whole line it works just as quickly with no limiter as it is optional? i see 1024, 4096 or so most often. it said in php.net to use 1024 instead of 4096 for small lines. but w/o a limiter there isnt an amount it goes to the very end of the line. so really speed isnt even effected for having no limiter since it would keep going even for smaller lines, as with bigger lines?
 

enouwee

Non ex transverso sed deorsum
Reaction score
240
The second argument (length) defaults to 1024 if you don't set a value yourself. If your lines are longer than that, PHP will waste time increasing the buffer.
 

alexho

New Member
Reaction score
0
ok thank you. thats what i suspected with my line maximums its under 1024 when i set them to default my page loaded 0.07 with smaller values it works the same with 0.03-4 it works more efficiently in my case with smaller lines.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • WildTurkey WildTurkey:
    is there a stephen green in the house?
    +1
  • The Helper The Helper:
    What is up WildTurkey?
  • The Helper The Helper:
    Looks like Google fixed whatever mistake that made the recipes on the site go crazy and we are no longer trending towards a recipe site lol - I don't care though because it motivated me to spend alot of time on the site improving it and at least now the content people are looking at is not stupid and embarrassing like it was when I first got back into this like 5 years ago.
  • The Helper The Helper:
    Plus - I have a pretty bad ass recipe collection now! That section of the site is 10 thousand times better than it was before
  • The Helper The Helper:
    We now have a web designer at my job. A legit talented professional! I am going to get him to redesign the site theme. It is time.
  • Varine Varine:
    I got one more day of community service and then I'm free from this nonsense! I polished a cop car today for a funeral or something I guess
  • Varine Varine:
    They also were digging threw old shit at the sheriff's office and I tried to get them to give me the old electronic stuff, but they said no. They can't give it to people because they might use it to impersonate a cop or break into their network or some shit? idk but it was a shame to see them take a whole bunch of radios and shit to get shredded and landfilled
  • The Helper The Helper:
    whatever at least you are free
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top