doutao6380 2012-07-06 11:32
浏览 10
已采纳

将配置文件读入PHP

I have a PHP file which writes some information into a configuration file which looks a bit like this...

#Date=01/2012
text1=hello
text2=goodbye

etc...

This file is written as a cfg file to be used by a perl script, but I need the file to be read back into PHP so it can be edited each time for the perl script to use again.

How can this be done?

I previously had it write into a .csv file and be read back in, but then was told this was not what was needed and it had to be in a cfg file, so will it be just as easy to add the info back into the PHP script as it is when reading in a .csv file.

I think what is needed is to not have the text1 and text2 variables to be read in, but to have what is after the equals to be read back in so the hello and goodbye strings would be read back into the PHP.

Another thing is that when the user adds in more info to say text1 it will have more than one value e.g. they could change text1 to...

text1=hello,howareyou

Then that , would need to be a seperate entity within the PHP script so I'm guessing I would use the explode function to get the seperate pieces of data.

This data will be read into a table where the user can then edit the information ready for when the perl script gets run again.

My HTML would be something like this when the info is read in.

<table>
    <tr>
        <td>text 1</td>
        <td>hello</td>
        <td>howareyou</td>
    </tr>
    <tr>
        <td>text 2</td>
        <td>goodbye</td>
    </tr>
</table>

I hope I have made myself clear with this and I am really struggling to comprehend how this could be done. As it doesn't seem as easy as reading from a .csv

Also I don't want the PHP to execute the script, I just need it to be read into the PHP so the user can edit the information again.

  • 写回答

2条回答 默认 最新

  • dpthuyh1678 2012-07-06 11:41
    关注

    Try this. You end up with a tidy associative array - param names as element names, and param values as element values.

    $cfg_path = 'path/to/config.cfg';
    if (file_exist($cfg_path)) {
        if (preg_match_all('/^(?!#)(.+)=(.*)$/m', file_get_contents($cfg_path), $cfg)) {
            $cfg = array_combine($cfg[1], $cfg[2]);
            foreach($cfg as $name => $val)
                echo $name.' = '.$val.'<br />';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程