How can i pick a number from file and row

i want to khow how to row a number from .txt file
something like
ex.txt
’  13096765   15823646   264669020   264669022   264669021   264669019  a 264669018   264669009  264669010 
264669017  264669016  ’

i want to row only number to table

hi unrockapple

I’m… having trouble understanding what you’re trying to do.

‘row’ isnt a verb (unless you’re in a boat.). what table are you referring to?

You’ve given us an example of your input (the file), but what output are you trying to get from it?

2 Likes

Try this:

<?php
  declare(strict_types=1);
  
  $lines = file( __FILE__ );
  foreach($lines as $id => $line) :
    echo $id .' ==> ' .$line ."<br>".;
  endforeach;  

Output:

Please take the time to lookup the excellent PHP Manual for the function details and parameters:

https://www.php.net/manual/en/function.file.php

1 Like

Sorry for word am not good in English

  1. I have log file .txt and like my example
    I want to manage them to table for export them to MS Excel

Put ; between the numbers and save the file with a .csv extension. Excel will open that just fine.

Some file more than 10,000 number

Excel can import with a space separator, can’t it? You can choose tab, semicolon, comma, space or specify by ASCII character code during the import loop.

Of course, you could adapt the code that John_Betong posted, explode each line into an array, and then call whatever Excel function with that array to write the new file format.

Try:

  1. a small sample from the original log file
  2. use the script I supplied
  3. create a $new_array outside of the foreach loop
  4. add a $new_array item with ;
  5. save the $new_array with a .csv extension using the following:

https://www.php.net/manual/en/function.file-put-contents.php

now i use this code

<?
$file = 'Reg.txt';
$current = file_get_contents($file);
preg_match_all('!\d+!', $current, $matches);
print_r($matches);
?>

result is

Array ( [0] => Array ( [0] => 5 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 10 [6] => 1 [7] => 0 [8] => 5 [9] => 13096765 [10] => 15823646 [11] => 264669020 [12] => 264669022 [13] => 264669021 [14] => 264669019 [15] => 264669018 [16] => 264669009 [17] => 264669010 [18] => 264669017 [19] => 264669016 ) )

but i want to separate them to

13096765 
15823646 
264669020 
264669022 
264669021 
264669019 
...

it’s possible to do separate number into the table

What do you mean about separate number into the table?
Please give more details and possibly an example.

Try this:

<?php
declare(strict_types=1); // PHP 7 OK otherwise error/warning shown.

//
// BEWARE
//  using proper-case file names can create problems
//
  $file     = 'reg.txt'; 
  $current  = file_get_contents($file);
  preg_match_all('!\d+!', $current, $matches);

// display array  of $matches 
   echo '<pre>'; // adds line feeds to make it easier to see
     print_r($matches);
   echo '</pre>';

// display one $match at a time
   foreach($matches[0] as $id => $match ) :
     echo '<br>'  ;
     echo '$id: ' .$id ;
     echo ' ==> ' .$match ;
   endforeach ;

Output:

Array
(
  [0] => Array
  (
    [0] => 13096765
    [1] => 15823646
    [2] => 264669020
    [3] => 264669022
    [4] => 264669021
    [5] => 264669019
    [6] => 264669018
    [7] => 264669009
    [8] => 264669010
    [9] => 264669017
    [10] => 264669016
   )
)


$id: 0 ==> 13096765
$id: 1 ==> 15823646
$id: 2 ==> 264669020
$id: 3 ==> 264669022
$id: 4 ==> 264669021
$id: 5 ==> 264669019
$id: 6 ==> 264669018
$id: 7 ==> 264669009
$id: 8 ==> 264669010
$id: 9 ==> 264669017
$id: 10 ==> 264669016
1 Like

Thank you so much it’s solve my problem

All Done !

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.