1

1) i have a text file (converted.txt) and that content's users info:

 0   hbhenet      pppoe   D4:CA:6D:F1:5D:5A 10.23.0.254     8h1m36s            
 1 R moh@hj       pppoe   00:27:22:16:3B:F2 10.17.0.253     8h1m36s            
 2 R omarmool@dr  pppoe   68:72:51:3A:53:1B 10.17.0.251     8h1m36s            
 3 R admin@khr2   pppoe   24:A4:3C:9F:83:10 10.17.0.252     8h1m36s            
 4 R [email protected]  pppoe   24:A4:3C:E6:DD:3E 10.17.0.250     8h1m36s            
 5 R [email protected]  pppoe   44:D9:E7:DA:32:95 10.16.0.246     8h1m36s        

2) and what i have done in my php is :

$file="converted.txt";
$fopen = fopen($file, "r");
$fread = fread($fopen,filesize($file));
fclose($fopen);

$remove = "\n";
$split = explode($remove, $fread);
$array[] = NULL;
$tab = "\t";

foreach ($split as $string)
{
    $row = explode($tab, $string);
    array_push($array,$row);
}

echo "<pre>";
print_r($array);
echo "</pre>";

3) the output is like :

Array
(

    [0] => 
[1] => Array
    (
        [0] =>  0   hbhenet      pppoe   D4:CA:6D:F1:5D:5A 10.23.0.254     8h1m36s            
    )

[2] => Array
    (
        [0] =>  1 R moh@hj       pppoe   00:27:22:16:3B:F2 10.17.0.253     8h1m36s            
    )

[3] => Array
    (
        [0] =>  2 R omarmool@dr  pppoe   68:72:51:3A:53:1B 10.17.0.251     8h1m36s            
    )

[4] => Array
    (
        [0] =>  3 R admin@khr2   pppoe   24:A4:3C:9F:83:10 10.17.0.252     8h1m36s            
    )

[5] => Array
    (
        [0] =>  4 R [email protected]  pppoe   24:A4:3C:E6:DD:3E 10.17.0.250     8h1m36s            
    )

[6] => Array
    (
        [0] =>  5 R [email protected]  pppoe   44:D9:E7:DA:32:95 10.16.0.246     8h1m36s        
    )

 )

4) but what i want is :

Array(

[0] => Array
    (
        [0] => Array
            (
                [0] => 0
                [1] => 
                [2] => hbhenet
                [3] => pppoe
                [4] => D4:CA:6D:F1:5D:5A
                [5] => 10.23.0.254
                [6] => 8h1m36s
            )
        [1] => Array
            (
                [0] => 1
                [1] => R
                [2] => moh@hj
                [3] => pppoe
                [4] => 00:27:22:16:3B:F2
                [5] => 10.17.0.253
                [6] => 8h1m36s
            )
        [2] => Array
            (
                [0] => 2
                [1] => R
                [2] => omarmool@dr
                [3] => pppoe
                [4] => 68:72:51:3A:53:1B
                [5] => 10.17.0.251
                [6] => 8h1m36s
            )
        [3] => Array
            (
                [0] => 3
                [1] => R
                [2] => admin@khr2
                [3] => pppoe
                [4] => 24:A4:3C:9F:83:10
                [5] => 10.17.0.252
                [6] => 8h1m36s
            )
        [4] => Array
            (
                [0] => 4
                [1] => R
                [2] => [email protected]
                [3] => pppoe
                [4] => 24:A4:3C:E6:DD:3E
                [5] => 10.17.0.250
                [6] => 8h1m36s
            )
        [5] => Array
            (
                [0] => 5
                [1] => R
                [2] => [email protected]
                [3] => pppoe
                [4] => 44:D9:E7:DA:32:95
                [5] => 10.16.0.246
                [6] => 8h1m36s
            )
    )
 )

any idea? thanks in advance.

2 Answers 2

4

I have created a file with name myfile.txt:-

 0   hbhenet      pppoe   D4:CA:6D:F1:5D:5A 10.23.0.254     8h1m36s            
 1 R moh@hj       pppoe   00:27:22:16:3B:F2 10.17.0.253     8h1m36s            
 2 R omarmool@dr  pppoe   68:72:51:3A:53:1B 10.17.0.251     8h1m36s            
 3 R admin@khr2   pppoe   24:A4:3C:9F:83:10 10.17.0.252     8h1m36s            
 4 R [email protected]  pppoe   24:A4:3C:E6:DD:3E 10.17.0.250     8h1m36s            
 5 R [email protected]  pppoe   44:D9:E7:DA:32:95 10.16.0.246     8h1m36s        

Now i have applied below code:-

<?php
$array = explode("\n", file_get_contents('myfile.txt'));

echo "<pre/>";print_r($array);

And output is :-

Array
(
    [0] =>  0   hbhenet      pppoe   D4:CA:6D:F1:5D:5A 10.23.0.254     8h1m36s            
    [1] =>  1 R moh@hj       pppoe   00:27:22:16:3B:F2 10.17.0.253     8h1m36s            
    [2] =>  2 R omarmool@dr  pppoe   68:72:51:3A:53:1B 10.17.0.251     8h1m36s            
    [3] =>  3 R admin@khr2   pppoe   24:A4:3C:9F:83:10 10.17.0.252     8h1m36s            
    [4] =>  4 R [email protected]  pppoe   24:A4:3C:E6:DD:3E 10.17.0.250     8h1m36s            
    [5] =>  5 R [email protected]  pppoe   44:D9:E7:DA:32:95 10.16.0.246     8h1m36s        
)

Now a workaround so that no need to change anything in your text file is given below (complete code):-

<?php
$array = explode("\n", file_get_contents('myfile.txt'));

echo "<pre/>";print_r($array);
$new_array = array();
foreach($array as $key =>&$arr){

    if ($arr[3] !=='R'){

        $arr[3] ="_";
    }
    $parts = preg_split('/\s+/', trim($arr));
    if($parts[1] == '_'){
        $parts[1] = '';
    }
    $new_array[$key] = $parts;
}

echo "<pre/>";print_r($new_array);

Note:- it will give you exact desired output what you shown to us (I have tested it at my local end)

What you asked in comment for that do like below:-

<?php
$array = explode("\n", file_get_contents('myfile.txt'));

echo "<pre/>";print_r($array);
$new_array = array();
$indexed_array = Array('number','flag','name','service','mac','IP','uptime');

foreach($array as $key =>&$arr){

    if ($arr[3] !=='R'){

        $arr[3] ="_";
    }
    $parts = preg_split('/\s+/', trim($arr));
    if($parts[1] == '_'){
        $parts[1] = '';
    }
    $new_array[$key] = array_combine($indexed_array,$parts);
}

echo "<pre/>";print_r($new_array);
Sign up to request clarification or add additional context in comments.

1 Comment

great job .. i really appreciated what you have done. thanks again @Anant
2
    <?php
    $lines = array();
    $fopen = fopen('converted.txt', 'r');
    while (!feof($fopen)) {
        $line=fgets($fopen);
        $line=trim($line);
        $lines[]=$line;

    }
    fclose($fopen);
    $finalOutput = array();
    foreach ($lines as $string)
    {
        $string = preg_replace('!\s+!', ' ', $string);
        $row = explode(" ", $string);
        array_push($finalOutput,$row);
    }
    echo "<pre>";
    print_r($finalOutput);
    echo "</pre>";
?>

and Output is

    Array
(
    [0] => Array
        (
            [0] => 0
            [1] => hbhenet
            [2] => pppoe
            [3] => D4:CA:6D:F1:5D:5A
            [4] => 10.23.0.254
            [5] => 8h1m36s
        )

    [1] => Array
        (
            [0] => 1
            [1] => R
            [2] => moh@hj
            [3] => pppoe
            [4] => 00:27:22:16:3B:F2
            [5] => 10.17.0.253
            [6] => 8h1m36s
        )

    [2] => Array
        (
            [0] => 2
            [1] => R
            [2] => omarmool@dr
            [3] => pppoe
            [4] => 68:72:51:3A:53:1B
            [5] => 10.17.0.251
            [6] => 8h1m36s
        )

    [3] => Array
        (
            [0] => 3
            [1] => R
            [2] => admin@khr2
            [3] => pppoe
            [4] => 24:A4:3C:9F:83:10
            [5] => 10.17.0.252
            [6] => 8h1m36s
        )

    [4] => Array
        (
            [0] => 4
            [1] => R
            [2] => [email protected]
            [3] => pppoe
            [4] => 24:A4:3C:E6:DD:3E
            [5] => 10.17.0.250
            [6] => 8h1m36s
        )

    [5] => Array
        (
            [0] => 5
            [1] => R
            [2] => [email protected]
            [3] => pppoe
            [4] => 44:D9:E7:DA:32:95
            [5] => 10.16.0.246
            [6] => 8h1m36s
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.