[英]How to get specific rows in a txt file in PHP?
Here is my problem, I am trying to make a script that create a new table in a database for each new txt file I get. 这是我的问题,我正在尝试制作一个脚本,以便为我得到的每个新txt文件在数据库中创建一个新表。 This part is ok but then I want to get some specific rows or words in the txt file to update the table. 这部分还可以,但是我想在txt文件中获取一些特定的行或单词以更新表。
Here is my code : 这是我的代码:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "exctraction";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
foreach (glob("*.txt") as $filename)
{
$sql = "CREATE TABLE IF NOT EXISTS `$filename` (
`id` int(100) unsigned NOT NULL AUTO_INCREMENT,
`device` varchar(30) NOT NULL,
`label` varchar(30) NOT NULL,
`configured` varchar(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;";
if ($conn->query($sql) === TRUE)
{
echo "<b>La table ".$filename." "." a été créée avec succès.</b> </br>";
}
else
{
echo "<u>Erreur lors de la création de la table :</u> </br>" . $conn->error;
}
$file = fopen( $filename, "r" );
$content = "";
for( $i = 0 ; $i < 158 ; $i++)
{
$content .= fgets($file, 200);
if($i==65)
{
$content_array = file($filename);
print_r ($content."</br>");
}
}
fclose($file);
}
?>
I want to display the lines I need and then make an UPDATE TABLE with these lines. 我想显示所需的行,然后使用这些行制作一个UPDATE TABLE。
The lines I want to get are from 57 to 65 in the txt file, but I am not able to get these only lines. 我要获取的行是txt文件中从57到65的行,但我无法仅获取这些行。
<?php
$File = "file.txt"; // I assume your file has 57th and 65th line
$fhandle = fopen($File, 'r');
while(!feof($fhandle)) // until end of file
{
$data[] = fgets($fhandle); // place each line to array
//Do whatever you want with the data in here
//This feeds the file into an array line by line
}
fclose($fhandle);
echo "<pre>"; // makes output easier to read
print_r($data[56].$data[64]); // outputs 57th and 65th line
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.