gpt4 book ai didi

php - 用PHP读取带有控制字符的TXT文件

转载 作者:行者123 更新时间:2023-11-29 09:56:32 27 4
gpt4 key购买 nike

如何在 PHP 中读取以下 TXT 文件: example.txt

#字符是控制(分隔)字符,例如2846是打印机ID,下面的数据是ID为2846的打印机的参数,必须读取:

ID:2846

+sn --> .1.3.6.1.4.1.367.3.2.1.2.1.4.0 = STRING:“已审查”

+m --> .1.3.6.1.4.1.367.3.2.1.1.1.1.0 = 字符串:“Aficio MP 171”

+s --> .1.3.6.1.4.1.367.3.2.1.1.1.2.0 = 字符串:“1.03”

...等+sn、+m等是打印机的参数。 @字符后可以读取代理的数据。 -- 字符是结束控制字符。然后这些参数应该保存在变量中,以便可以加载到mysql表中。

我尝试编写脚本,但我不知道如何读取该文件。

<?php

$conn = mysqli_connect('localhost','root','','demo');
if(!$conn) die(mysqli_error());

$open = fopen('example.txt','r');
while (!feof($open)) {
$getTextLine = fgets($open);

/* reading txt file, separating values of control parameters of printer
the code...
*/

list($ID,$sn,$m,$s,...etc) = $explodeLine;
$qry = "insert into agent_name (ID,sn,m,s,...etc) values('".$ID."','".$sn."','".$m."','".$s.",...etc')";
mysqli_query($conn,$qry);
}

fclose($open);

?>

最佳答案

此例程将从文件中读取行并将其构建为行数组,然后当它到达仅包含 # 的行时,它将处理数据。

数据被分割,以便首先获得 ID,然后它选择标志的交替行,后跟设置,并创 build 置的关联数组。

$open = fopen('example.txt','r');
fgets($open); // Ignore first line
$entry = [];
while (!feof($open)) {
$getTextLine = trim(fgets($open));

if ( $getTextLine == '#' ) {
// Take ID as first line
$settings = ['ID' => array_shift($entry)];
for ( $i = 0; $i < count($entry); $i+=2 ) {
$settings[ltrim($entry[$i],"+")] = $entry[$i+1]??'';
}

print_r($settings);
// list($ID,$sn,$m,$s,...etc) = $explodeLine;
// $qry = "insert into agent_name (ID,sn,m,s,...etc) values('".$ID."','".$sn."','".$m."','".$s.",...etc')";
// mysqli_query($conn,$qry);
$entry = [];
}
else {
$entry[] = $getTextLine;
}
}

目前它打印出数据,但您应该能够将其插入数据库。尽管我建议查看准备好的语句,以确保没有引号或其他奇怪的情况导致 SQL 出现问题。

第一个条目的输出示例...

Array
(
[ID] => 2846
[sn] => .1.3.6.1.4.1.367.3.2.1.2.1.4.0 = STRING: "censored"
[m] => .1.3.6.1.4.1.367.3.2.1.1.1.1.0 = STRING: "Aficio MP 171"
[s] => .1.3.6.1.4.1.367.3.2.1.1.1.2.0 = STRING: "1.03"
[ff] => .1.3.6.1.4.1.367.3.2.1.2.19.5.1.9.1 = INTEGER: 254623
[fffc] => .1.3.6.1.4.1.367.3.2.1.2.19.5.1.9.23 = INTEGER: 746801
[ffny] => .1.3.6.1.4.1.367.3.2.1.2.19.5.1.9.6 = INTEGER: 230398
[ffm] => .1.3.6.1.4.1.367.3.2.1.2.19.5.1.9.19 = INTEGER: 19932
[scff] => .1.3.6.1.4.1.367.3.2.1.2.19.5.1.9.29 = INTEGER: 4476
[scsz] => .1.3.6.1.4.1.367.3.2.1.2.19.5.1.9.28 = INTEGER: 3549
[fb] => .1.3.6.1.4.1.367.3.2.1.2.24.1.1.5.1 = INTEGER: -3
)

更新了代码...

$open = fopen('example.txt','r');
fgets($open); // Ignore first line
$entry = [];
$all_settings = [];
while (!feof($open)) {
$getTextLine = trim(fgets($open));

if ( $getTextLine == '#' ) {
// Take ID as first line
$settings = ['ID' => array_shift($entry)];
for ( $i = 0; $i < count($entry); $i+=2 ) {
$settings[ltrim($entry[$i],"+")] = $entry[$i+1]??'';
}

$all_settings [] = $settings;
$entry = [];
}
else {
$entry[] = $getTextLine;
}
}

print_r($all_settings);

关于php - 用PHP读取带有控制字符的TXT文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53745751/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com