使用 php 编辑文本文件中的行组

Posted

技术标签:

【中文标题】使用 php 编辑文本文件中的行组【英文标题】:edit group of line in the text file using php 【发布时间】:2014-03-23 03:28:41 【问题描述】:

我有一个文本文件,顶部和底部都有公司详细信息。在中间,它有这样的所有员工详细信息

Company name: ABC Group of company
Company Location: AYZ
Company Address: XYS,YTS,123
Company found: 2013
Company website: abc.com
Company email: email@abc.com

Employee number: 001
Employee name: ABC
Employee phone number: 123456789
Employee address: XYX, HHH,123
employee date of birth:xxxx
employee email:xxxx
employee joined date:xxxx
employee position: manager

Employee number: 003
Employee name: ABC
Employee phone number: 123456789
Employee address: XYX, HHH,123
employee date of birth:xxxx
employee email:xxxx
employee joined date:xxxx
employee position: manager  

Employee number: 004
Employee name: ABC
Employee phone number: 123456789
Employee address: XYX, HHH,123
employee date of birth:xxxx
employee email:xxxx
employee joined date:xxxx
employee position: manager  

Employee number: 011
Employee name: ABC
Employee phone number: 123456789
Employee address: XYX, HHH,123
employee date of birth:xxxx
employee email:xxxx
employee joined date:xxxx
employee position: manager  

Employee number: 022
Employee name: ABC
Employee phone number: 123456789
Employee address: XYX, HHH,123
employee date of birth:xxxx
employee email:xxxx
employee joined date:xxxx
employee position: manager

Employee number: 044
Employee name: ABC
Employee phone number: 123456789
Employee address: XYX, HHH,123
employee date of birth:xxxx
employee email:xxxx
employee joined date:xxxx
employee position: manager

Employee number: 009
Employee name: ABC
Employee phone number: 123456789
Employee address: XYX, HHH,123
employee date of birth:xxxx
employee email:xxxx
employee joined date:xxxx
employee position: manager  

Employee number: 031
Employee name: ABC
Employee phone number: 123456789
Employee address: XYX, HHH,123
employee date of birth:xxxx
employee email:xxxx
employee joined date:xxxx
employee position: manager

company chairman name:jjjj
company type of business:ffffff

我需要使用以下功能创建一个 php 页面

list all employee details
edit a employee details
delete a employee details
add new employee details

如果这些信息存储在数据库中,我可以轻松做到。 我认为通过逐行阅读,我可以轻松列出所有员工的详细信息。

1) is there any other best way to do listing?
2) how can i edit/delete/add employee details?

你能帮忙吗?

谢谢

【问题讨论】:

您不能使用数据库有什么真正的原因吗?这可以在文件中完成,但实际上不是最好的方法。 感谢您的快速回复。文本文件被另一个应用程序使用。我们需要开发网络应用程序来编辑它。 其他应用程序不能存储到数据库吗?如果没有,那么您想阅读有关在 PHP 中访问文件的信息。 uk3.php.net/manual/en/function.file.php 没有。需要使用文本文件 那么没有问题。正如我在上一条评论中所说,您需要阅读有关 PHP 文件函数的信息。一旦您尝试并获得了一些代码,我们就可以提供帮助。 【参考方案1】:

将数据读入数据库是很有趣的部分,因为如果你犯了错误,你可以截断表。

但是做 CRUD(创建读取更新删除)并不是那么有趣,很多基本的乏味工作在这里没有人会给你代码或为你完成解决方案。如果你想成为一名优秀的程序员,你需要一直自学和自学!

我确信那里有更好的资源,但是 w3schools 的速成课程肯定会让你上路。

1. PHP:http://www.w3schools.com/php/default.asp

2。 mysql:http://www.w3schools.com/php/php_mysql_intro.asp

3. PHP mySQLi 分机: http://www.w3schools.com/php/php_ref_mysqli.asp

4. JavaScript、jQuery 和 AngularJS:http://www.w3schools.com/js/default.asp

好的,使用一些免费代码将数据读入您的数据库:

http://phpfiddle.org/main/code/ix3s-bmzz

$lines = file("employeedata.txt");

$final_company = array();
$final_employees = array();
$employee = array();
foreach($lines as $line)

    $line = str_replace("\n","",$line); // Clear end of line character

    if ($line != "")   // Check for blank line (indicator of next record)
        $arr = explode(":",$line);
        $fields = explode(" ",$arr[0]);
        $vals = $arr[1];
        switch(strtolower($fields[0])) 
            case "company" : $final_company[$fields[1]] = $vals;
            break;
            case "employee" : $employee[$fields[1]] = $vals;
            break;
        
     else         
        if (!empty($employee)) array_push($final_employees, $employee);
        $employee = array(); // Clear array for next employee
    

if (!empty($employee)) array_push($final_employees, $employee); //Catch last entry

// Enter your DB INSERT statement here, following is a dump of the arrays

echo "<pre> <b>COMPANY DATA:</b><br/>";
print_r($final_company); 
echo "<b>EMPLOYEE DATA:</b><br/>";
print_r($final_employees);
echo "</pre>";

【讨论】:

以上是关于使用 php 编辑文本文件中的行组的主要内容,如果未能解决你的问题,请参考以下文章

将文本从文件复制到另一个文件并编辑生成的文件

PHP删除TXT文本内容

vim文本编辑

python:删除重复的文本行组

linux shell 之在线文本编辑sed

使 sed 替换文件中的文本,并在屏幕上显示替换的行