简体   繁体   中英

PHP: Code get links from X lines to Y Lines doesn't work

This is code I used to get lines between X lines and Y lines:

$files = new SplFileObject($filename);
$fileIterator = new LimitIterator($files, 1800, 2000);

I want to get 200 lines from 1800 to 2000. However, when I print array, it shows 2000 lines. So, what wrong I do ?

The second parameter is a count, you want 200 not 2000

$fileIterator = new LimitIterator($files, 1799, 200);

You can check it from the LimitIterator Doc :

public __construct ( Iterator $iterator [, int $offset = 0 [, int $count = -1 ]] )

Also consider that offset starts from 0, so if you want the 1800th line, you need to use 1799

When you look at the documentation you can see that the second parameter is a count and not the ending line.

public __construct ( Iterator $iterator [, int $offset = 0 [, int $count = -1 ]] )

http://php.net/manual/en/class.limititerator.php

$fileIterator = new LimitIterator($files, 1800, 200);

So you need something like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM