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 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗