Reading a CSV with file_get_contents in PHP
I am reading a 'kind' of csv file and exploding it and storing it in array.
The file I am reading has this structure
Id,Log,Res,File
mydb('Test1','log1','Pass','lo1.txt').
mydb('Test2','log2','Pass','lo2.txt').
mydb('Test3','log3','Pass','lo3.txt').
Now开发者_运维知识库 what I am trying to do is : reading the last record in my database, get the Name, lets say in this case 'Test1' and then I am searching through my file and where I can find the position of 'Test1' and get the next lines in the file, extract the ID,s and add it to database.
I am getting the position of desired string in the file, but I am not sure how to get the next lines too.
Here's my code so far.
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("testing") or die(mysql_error());
$result = mysql_query("select ID from table_1 order by S_no DESC limit 1") or die(mysql_error());
$row = mysql_fetch_array( $result );
$a = $row['ID'];
echo 'Present Top Row is '.$a.'<br>';
$addresses = explode("\n", file_get_contents('\\\\fil1\\logs\\tes.pl'));
foreach($addresses as $val)
{
$pos = strstr($val, $a);
if ($pos === false) {
} else {
echo "The string <br> '$a' <br>was found in the string <br>'$val' <br>";
echo " and exists at position <br>$pos<br>";
}
}
Hope I understand you correctly. If so you could just set a flag once the item is found and then explode each line then on a ' character. The second value in the resultant array should then contain the id as per your example.
$addresses = explode("\n", file_get_contents('\\\\fil1\\logs\\tes.pl'));
$bFound = false;
foreach($addresses as $val)
{
if (!$bFound) {
$pos = strstr($val, $a);
if ($pos === false) {
} else {
echo "The string <br> '$a' <br>was found in the string <br>'$val' <br>";
echo " and exists at position <br>$pos<br>";
$bFound = true;
}
}
else {
$aCols = explode("'", $val);
$sId = $aCols[1];
// Now add Id as stored in $sId to DB here
}
}
If id
is supposed to be unique, the traffic between the php process and the MySQL server is not the limiting factor and you have to parse each line anyway, you could create a unique index and let MySQL decide whether it accepts the record or not.
Simple example:
$pdo = new PDO('mysql:host=localhost;dbname=test', 'localonly', 'localonly');
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$pdo->exec('CREATE TEMPORARY TABLE foo (Id varchar(16),Log varchar(16),Res varchar(16),File varchar(16), unique key(Id))');
$stmt = $pdo->prepare('INSERT IGNORE INTO foo (Id,Log,Res,File) VALUES(?,?,?,?)');
$pattern = '/(?<=^mydb\().+(?=\)\.$)/';
$fp = 'dummy';
while ( false!==($row=dummy_fgets($fp)) ) {
if ( preg_match($pattern, $row, $m) ) {
$row = str_getcsv($m[0], ',', "'");
$stmt->execute($row);
echo $row[0], ' -> ', $stmt->rowCount(), "\n";
}
}
function dummy_fgets($dummy) {
static $data = array(
"mydb('Test1','log1','Pass','lo1.txt').\n",
"mydb('Test2','log2','Pass','lo2.txt').\n",
"mydb('Test3','log3','Pass','lo3.txt').\n",
// start again ...like you process the same file again + 1 new record
"mydb('Test1','log1','Pass','lo1.txt').\n",
"mydb('Test2','log2','Pass','lo2.txt').\n",
"mydb('Test3','log3','Pass','lo3.txt').\n",
"mydb('Test4','log3','Pass','lo3.txt').\n"
);
$rv = current($data);
next($data);
return $rv;
}
prints
Test1 -> 1
Test2 -> 1
Test3 -> 1
Test1 -> 0
Test2 -> 0
Test3 -> 0
Test4 -> 1
精彩评论