php file()函数

file() — 把整个文件读入一个数组中

使用使用方法:

file(filename,[$flags]);

可选参数 flags 可以是以下一个或多个常量:

FILE_USE_INCLUDE_PATH   //在 include_path 中查找文件。
FILE_IGNORE_NEW_LINES   //在数组每个元素的末尾不要添加换行符
FILE_SKIP_EMPTY_LINES  //跳过空行

例如  filename.zl 文件中保存如下内容:

first_line =  one
second_line = two
third_line = three

代码如下:

$filename = filename.zl;
$arrLine = file($filename);
var_dump($arrLine);   // 文件中的每一行就是数组的一个值

下面给一个Yii2 中获取文件的方法:

    /**
     * 读取文件中配置项值
     * @param null $option
     * @param null $fileName
     * @return array|mixed|null
     * @throws Exception
     */
    public static function getFileKey($option = null, $fileName = null)
    {
        $fileName = (($fileName !== null)) ? $fileName : \Yii::$app->params['file_url'];
        $fileName = \Yii::getAlias($fileName);
        $arrLine = file($fileName);
        $arrFile = [];
        foreach ($arrLine as $line) {
            $arrTemp = explode(' = ', $line);
            $key = trim($arrTemp[0]);
            $arrFile[$key] = trim($arrTemp[1]);
        }
        if ($option !== null) {
            $value = (isset($arrLicense[$option])) ? $arrLicense[$option] : null;
        } else {
            $value = $arrFile;
        }
        return $value;
    }

daicr工作中的总结:http://www.cnblogs.com/chrdai/p/8856285.html

 

猜你喜欢

转载自www.cnblogs.com/chrdai/p/8856285.html
今日推荐