简体   繁体   中英

How can i remove other lines from a csv formatted string using PHP?

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 :)

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 
        // -------
    ?>

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