Read line by line from TXT and return at end (PHP)

Asked

Viewed 731 times

1

I have the following function:

function pegarWS($numero){

    $PEGAR_WS = 'http://ws.com.br';
    $URL = $PEGAR_WS.$numero;

$proxies = file('pasta/secreta/inacessivel/via/http/proxies.txt');

//Limpa espaços em branco
$proxies = array_map(function ($proxy) {
    return trim($proxy);
}, $proxies);

// pegar random proxy


 $proxy_to_use = $proxies[ rand( 0, count( $proxies ) -1 ];

foo

In particular: (I need to adapt)

$proxy_to_use = $proxies[ rand( 0, count( $proxies ) -1 ];

Instead of searching for the proxy form Random (see code), I need to change it so that it reads line by line from txt (one at a time) in loop. That is, read the first line, then the second and so on.. When you get to the last line it goes back to the first line.

  • It is not clear what you need or what error you are getting

  • @Jorgecosta, I left the most detailed question.

  • Why not put the code inside the map array ?

  • @Jorgecosta, I stopped where the question is :-( I can’t read line by line.

  • 1

    To see if I realized every time I invoke pegWS you want to return a line from the file sequentially

  • @Jorgecosta.

  • 1

    Then you will have to keep an index of the last line used and maybe also the array of lines to not always read the file (best performance)

  • But do you want to recover those lines within the file? Or every time you call the function it returns a specific line?

  • @Andreicoelho, each time he opens the file, he returns the next line and when he reaches the last line, he goes back to the first.

  • I had asked the wrong question. What I wanted to know is if you want to recover these lines within the function or if each time you call the function it returns the line... But I believe it’s the second option.

  • 1

    Was that it? Retrieve line by line every function call?

  • That’s exactly it. I’m just not getting it into my code...

  • There’s another option. I’ll post it to you.

  • Try the option using session.

Show 9 more comments

1 answer

1


If I understand correctly, you can use recursion to do this. This way it will always return the next line and return to the first one whenever file reaches the end. The limit is the for.

function pegarWS($numero){

  $file = file("pasta/secreta/inacessivel/via/http/proxies.txt"); 
  if(isset($file[$numero])){
      return $file[$numero];
  }
  // se não existir o índice subtrai o número
  // envia para a mesma função até chegar ao índice 0
  return pegarWS ($numero - count($file));

}

for($i = 0; $i < 10; $i++){
    echo pegarWS($i);
}

A suggestion

Open the file only once.

$file = file("pasta/secreta/inacessivel/via/http/proxies.txt"); 
$linhas = [];
foreach($file as $linha){
    $linhas[] = $linha;
}

$y = 0;
for($i = 0; $i < 10; $i++){
    if(!isset($linhas[$y])){
        $y = 0;
    } 
    echo $linhas[$y];
    $y++;
}

Other option

Using session to save indexes

function pegarWS($numero){

    $PEGAR_WS = 'http://ws.com.br';
    $URL = $PEGAR_WS.$numero;

    $proxies = file('pasta/secreta/inacessivel/via/http/proxies.txt');

    //Limpa espaços em branco
    $proxies = array_map(function ($proxy) {
        return trim($proxy);
    }, $proxies);

    if(!isset($_SESSION['index'])){
        $index = $_SESSION['index'] = 0;
    } else {
        $index = $_SESSION['index'];
    }
    if(isset($proxies[$index])){
        $linha = $proxies[$index];
    } else {
        $linha = $proxies[0];
        $_SESSION['index'] = 0;
    }
    $_SESSION['index']++;

    $proxy_to_use = $linha;

    // ... resto do código
}
  • ~ Straight! Thank you!

  • @J.Doe quiet!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.