0

Hi I have a string which is in a csv format,

Lee.leviste,112.198.77.139:44324,12872826,82159116,Thu Dec 15 16:30:18 2016
jelozero23,112.198.78.211:32704,157357727,2952656671,Thu Dec 15 10:59:48 2016
walangmaypake,112.198.78.167:22756,2338395760,27337738911,Thu Dec 15 14:20:12 2016
ROUTING TABLE
Virtual Address,Common Name,Real Address,Last Ref
10.4.0.246,Lee.leviste,112.198.77.139:44324,Thu Dec 15 19:50:26 2016
10.4.0.202,jelozero23,112.198.78.211:32704,Thu Dec 15 19:50:59 2016
10.4.0.250,walangmaypake,112.198.78.167:22756,Thu Dec 15 19:51:00 2016

How can I remove those lines starting from ROUTING TABLE upto the last line and get this output?

Lee.leviste,112.198.77.139:44324,12872826,82159116,Thu Dec 15 16:30:18 2016
jelozero23,112.198.78.211:32704,157357727,2952656671,Thu Dec 15 10:59:48 2016
walangmaypake,112.198.78.167:22756,2338395760,27337738911,Thu Dec 15 14:20:12 2016

Thanks in advance :)

  • did you use `fgetcsv()` function to get the data from CSV? If so then the data should be an array now then you must have used `while loop` to get the data from each row and column of the CSV file so what you need to do from that point (considering that `ROUTING TABLE` is in the first column of the CSV) is to check if the first column is equal to `ROUTING TABLE`, if it is equal then exit the loop. This only applies if you have used `fgetcsv()` function but if not then don't mind this solution. – Mark Vincent Manjac Dec 16 '16 at 01:21
  • Thanks, I will try to use fgetcsv(). – John Carlo Hermosa Olideles Dec 16 '16 at 05:25

1 Answers1

0

If all you need is to take a string and remove all content after "ROUTING TABLE" then this would work. However it looks like you may want to use it as a CSV after? How are you converting it to a string? Depending on how you are using it you may want to look into converting it into an array from the CSV file and then it would be done slightly different.

<?php 

        $string = "Lee.leviste,112.198.77.139:44324,12872826,82159116,Thu Dec 15 16:30:18 2016
                    jelozero23,112.198.78.211:32704,157357727,2952656671,Thu Dec 15 10:59:48 2016
                    walangmaypake,112.198.78.167:22756,2338395760,27337738911,Thu Dec 15 14:20:12 2016
                    ROUTING TABLE
                    Virtual Address,Common Name,Real Address,Last Ref
                    10.4.0.246,Lee.leviste,112.198.77.139:44324,Thu Dec 15 19:50:26 2016
                    10.4.0.202,jelozero23,112.198.78.211:32704,Thu Dec 15 19:50:59 2016
                    10.4.0.250,walangmaypake,112.198.78.167:22756,Thu Dec 15 19:51:00 2016";

        $string = substr($string, 0, strpos($string, "ROUTING TABLE"));

        echo "-------<br>";
        echo $string;
        echo "<br>-------";

        // OUTPUT:
        // -------
        // Lee.leviste,112.198.77.139:44324,12872826,82159116,Thu Dec 15  16:30:18 2016 jelozero23,112.198.78.211:32704,157357727,2952656671,Thu Dec 15 10:59:48 2016 walangmaypake,112.198.78.167:22756,2338395760,27337738911,Thu Dec 15 14:20:12 2016 
        // -------
    ?>
Icewine
  • 1,851
  • 1
  • 12
  • 22