Reputation: 117
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()\n";
}
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.
Upvotes: 0
Views: 86
Reputation: 366
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.
Upvotes: 1