文本文件数据导入到 php
Text file data importing to php
我是 php 编码的新手。所以我的问题来了,我需要获取文本文件中的数据。
我有一个文本文件,其中的数据是这样的:
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"
所以我必须根据名称、时间、状态等类别获取引号内的数据。
您可能需要稍微调整一下日期字段。简单解析器:
$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 );
时间破解:
<?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 );
?>
我是 php 编码的新手。所以我的问题来了,我需要获取文本文件中的数据。 我有一个文本文件,其中的数据是这样的:
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"
所以我必须根据名称、时间、状态等类别获取引号内的数据。
您可能需要稍微调整一下日期字段。简单解析器:
$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 );
时间破解:
<?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 );
?>