dongtan1009 2014-12-03 16:06
浏览 72
已采纳

将PHP文本文件解析为多个变量

I'm trying to parse a TXT file that contains different kind of configuration (Routers configuration), for example like this:

    Router model C1i IPSec CR 1 95 CPU MPC860      S/N: 444/33333
    1 LAN, 1 ISDN Line, 1 XDSL Line
    CIT software version: 10.4.2.0.3 Oct 22 2004 10:24:51


    Router *
    Router *p 3
    Console Operator
    Router +net atm

    -- ATM Console --
    Router atm0/0 monitor+list all

    Description: ADSL over POTS (Alcatel DynaMiTe P) over Motorola MPC860 SAR

    ---- Status ----
    Transmission:       ENABLED     Phy rate:        320 kbps
    Reception:          ENABLED     Phy rate:       3008 kbps
    Status:                  UP     for       0/04:10:54 (days/hh:mm:ss)

    ---- Statistics ----
    Tx user cells        =     308481         Rx user cells        =    5059007
    Tx no user cells     =          0         Rx no  user cells    =          0
                                              Rx invalid           =          0
                                              Rx missinserted      =          0
                                              Rx future functions  =          0
    Tx bytes             =   16349493         Rx bytes             =  268127371
    Tx last 5 min (kbps) =          0         Rx last 5 min (kbps) =          0

    Linked structs       =        100 (10 free)

I want to parse from PHP, detecting interesting info, like Serial Number, disposition of ports, ATM speed, etc.

The problem is that I don't know how detect the different words, for example S/N, "Transmission", "Reception", "Status". There is a function called strpos, but it is only for one term, not for various.

    <?php


    if ($gestor = (@fopen("documento.txt", "r")))
            {
            while (($bufer = fgets($gestor, 4096)) !== FALSE) 
                {
                switch ($bufer)
                    {
                    case "Router Model":
                    echo"DETECTADO";
                    break;
                    default:
                    echo $bufer;
                    break;
                    }

                echo "<br />";
                }
            if (!feof($gestor)) {
                echo "Error: fallo inesperado de fgets()
";
            }
            fclose($gestor);
            }
        else
            {
        echo "Error al abrir el fichero";

            }

    ?>

The last option I have is parsing the entire TXT file for each variable I want to store, but is not efficient.

Thank you.

  • 写回答

1条回答 默认 最新

  • doute3621 2014-12-03 16:11
    关注

    At the end of the day your going to need a parser.

    my recommendation is something like this:

    Find all the words in the text file that has a ":" at the end of it. (Being the important so called headers for interesting data) Once you do that find the value to the right of it minus any spaces and you have your value to store.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 计算二重积分∫∫e^(x+y)dxdy,其中0≤x≤1,0≤y≤1,试分别用复合辛普森公式(取n=4)以及高斯求积公式(取n=4)计算积分 给出matlab程序
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的