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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里