Reputation: 9
I am newbie to php coding. So here my problem goes, I need to get the data on a textfile. I have a text file like where data are like this:
Return(result="success" dev_id="6714114030000088" total="14"
time="2015-02-11 11:35:20" id="505" name="DIM" workcode="0" status="0" authority="0X11" card_src="from_check"
time="2015-02-11 11:42:47" id="505" name="DIM" workcode="0" status="0" authority="0X11" card_src="from_check"
So I have to get data inside the quotation marks according to its category like name, time, status, etc.
Upvotes: 0
Views: 198
Reputation: 1930
You may need to tweak the date field a bit. Simple parser:
$aFile = file( 'infile.txt' );
$iCountLines = count( $aFile );
$aData = array();
for( $i = 0; $i < $iCountLines; ++$i )
{
// Skip empty lines.
if( empty( trim( $aFile[ $i ] ) ) )
{
continue;
}
// Skip header line. Can extend to skip unnecessary lines.
if( strpos( $aFile[ $i ], 'Return(result' ) !== FALSE )
{
continue;
}
// Separate based on space. Time values will be distorted.
$aRaw = explode( ' ', $aFile[ $i ] );
$iCountRaw = count( $aRaw );
// Reset temp data.
$aTmp = array();
for( $j = 0; $j < $iCountRaw; ++$j )
{
$aTmpRaw = explode( '=', $aRaw[ $j ] );
// Time exclusion
if( !empty( $aTmpRaw[ 1 ] ) )
{
$aTmp[ $aTmpRaw[ 0 ] ] = $aTmpRaw[ 1 ];
}
}
$aData[] = $aTmp;
}
var_dump( $aData );
Time hack:
<?php
$aFile = file( 'infile.txt' );
$iCountLines = count( $aFile );
$aData = array();
for( $i = 0; $i < $iCountLines; ++$i )
{
// Skip empty lines.
if( empty( trim( $aFile[ $i ] ) ) )
{
continue;
}
// Skip header line. Can extend to skip unnecessary lines.
if( strpos( $aFile[ $i ], 'Return(result' ) !== FALSE )
{
continue;
}
// Separate based on space. Time values will be distorted.
$aRaw = explode( ' ', $aFile[ $i ] );
// Re-connect time values.
$sTmp = $aRaw[ 0 ];
$sTmp2 = $aRaw[ 1 ];
unset( $aRaw[ 0 ] );
unset( $aRaw[ 1 ] );
$aRaw[ 0 ] = $sTmp . $sTmp2;
$aRaw = array_values( $aRaw );
$iCountRaw = count( $aRaw );
// Reset temp data.
$aTmp = array();
for( $j = 0; $j < $iCountRaw; ++$j )
{
$aTmpRaw = explode( '=', $aRaw[ $j ] );
// Time exclusion
if( !empty( $aTmpRaw[ 1 ] ) )
{
$aTmp[ $aTmpRaw[ 0 ] ] = $aTmpRaw[ 1 ];
}
}
$aData[] = $aTmp;
}
var_dump( $aData );
?>
Upvotes: 1