gpt4 book ai didi

PHP 解析 .txt

转载 作者:可可西里 更新时间:2023-11-01 08:29:15 26 4
gpt4 key购买 nike

我在网站上工作,希望管理员能够上传包含员工 ID 和姓名的 .txt 文件。我需要解析 .txt 文件并将信息插入数据库,但我在解析时不断出错。

我尝试遵循 PHP - parsing a txt file 的格式,但在我的第一个 $row explode() 之后,我开始遇到几个非法或未定义的偏移错误。

.txt:

186298  "Cushing,Kathy N"
167876 "Roberts,Joseph Allen"
109876 "Smith,Sarah Quinn"
118679 "Hernandez,Juan"
187568 "Freeman,Colby Matthew"

.php 函数:

function updateMemberList()
{
global $db_handle;
$tmpName = $_SESSION['filename'];
//$include_once ($_SERVER['DOCUMENT_ROOT'] . "../../uploads/updates/'$tmpName'");
$txt_file = file_get_contents("../../uploads/updates/$tmpName");
$rows = explode("\n", $txt_file);

foreach($rows as $row => $data)
{
//Splits data into employee ID and full name
$row_data = explode(' ', $data);

$info[$row]['empid'] = $row_data[0];
$info[$row]['name'] = $row_data[1]; //AFTER this line is where I start having errors

//Splits name into last name and first name
$row_name = explode(',', $info[$row]['name']);
$info[$row_name]['lname'] = $row_name[0];
$info[$row_name]['fname'] = $row_name[1];

//Cleans " off last name
$row_lname = explode('"', $info[$row_name]['lname']);
$info[$row_lname]['lname'] = $row_lname[1];

//Cleans middle name and " off first name
$row_fname1 = explode(' ', $info[$row]['fname']);
$info[$row]['fname'] = $row_fname1[0];
$row_fname2 = explode('"', $info[$row]['fname']);
$info[$row]['fname'] = $row_fname2[0];

//Declares variables
$uname = $info[row]['fname'] + "." + $info[$row]['lname'];
$fname = $info[row]['fname'];
$lname = $info[$row]['lname'];
$empid = $info[$row]['empid'];

//Checks to see if user is already in db
$query = ("SELECT * FROM user WHERE username = '$uname'");
$check = mysqli_query($db_handle, $query);
$num_rows = $check->num_rows;

//If user isn't in db, generates a password and adds them
if ($num_rows < 1)
{
//Generates random 8 character password
$length = 8;
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
for ($i = 0; $i < $length; $i++)
{
$randomString[$i] = $characters[rand(0, $charactersLength - 1)];
}
$temppass = implode($randomString);
$pword = password_hash($temppass, PASSWORD_DEFAULT);
$addto = "INSERT INTO `user`(`username`, `first_name`, `last_name`, `employee_id`, `password_hash`, `isActive`, `firstLogin`) VALUES ('$uname', '$fname', '$lname', '$empid', '$pword', 1, 1)";
mysqli_query($db_handle, $addto);
}
}
}

有没有什么方法可以更简单地获取名字和姓氏并且不会对我大喊大叫?

编辑:通过交换为 .csv 而不是 .txt 解决了问题

最佳答案

使用正则表达式它使它变得更简单而且你不需要 explode('\n') 只需要 file() 函数

  foreach($rows as $row => $data) { 
$matches = array();
preg_match('#([0-9]+).*? "([^""]+?)"#', $data, $matches);
echo $matches[1]; //id
echo $matches[2]; //name
}

你也可以改变

    $txt_file = file_get_contents("../../uploads/updates/$tmpName");
$rows = explode("\n", $txt_file);

    $rows = file("../../uploads/updates/$tmpName");

此外,生成随机的 8 长度字符串使用

$temppass = bin2hex(openssl_random_pseudo_bytes(4));

它更安全。

关于PHP 解析 .txt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31455981/

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