【问题标题】:control file to PHP array控制文件到 PHP 数组
【发布时间】:2010-12-08 22:13:19
【问题描述】:

在 Linux 中,.DEB 文件具有如下排列的“控制”文本文件:

Name: Value
Size: Value
Information: Mutliline
             value

将控制文件放入类似 PHP 数组的最佳方法是:

Array ( "Name" => Value, "Size" => Value, "Information" => Value);

请记住,这些值可以是多行的,并且包括“:”分隔符。

谢谢!

【问题讨论】:

  • 太糟糕了,它似乎是一种专有格式,而不是像 YAML 这样的格式,至少不是根据这个debian.org/doc/debian-policy/ch-controlfields.html
  • 从我的(有限的)工作打包 .debs 中,我认为它遵循与 HTTP 标头相同的语法......也许有一个 PHP 库可以解析这些?
  • @Pekka:再来一次?它是基本的 RFC822 格式。

标签: php deb


【解决方案1】:
$source = fopen('path/to/file');
$index = '';
while( ($line = fgets($source)) !== false ){
    if(preg_match('/^\s*$/', $line))
        continue 1; // ignore empty lines //

    if(!preg_match('/^\s+/', $line)){ // if the line does not start with whitespace then it has a new key-value pair //
        $items = explode(':', $line, 2); // separate at the first : //
        $index = strtolower($items[0]); // the keys are case insensitive //
        $value = preg_replace('/^\s+/', '', $items[1]); // remove extra whitespace from the begining //
        $value = preg_replace('/\s+$/', '', $value); // and from the end //
    }
    else{ // continue the value from the previous line //
        $value = preg_replace('/\s+$/', '', $line); // remove whitespace only from the end //
    }
    $data[$index] .= $value;
}
fclose($source);

按照此处所述实现:http://www.debian.org/doc/debian-policy/ch-controlfields.html

如有错误欢迎指正!

【讨论】:

  • 经过一系列控制文件的测试,似乎运行良好!谢谢!
【解决方案2】:

//图像属性是输入文件字段中的名称属性,就像用于发布方法的名称一样

// return array of errros
public function Controle_upload_file($file_field_name,$max_photo_uploading_size=10485760,$allow_exts=["JPG","GIF","PNG","JPEG"],$request_type="POST"){
    //fix bytes shows
    $errors=array();
    if($_SERVER['REQUEST_METHOD'] == $request_type && isset($_FILES[$file_field_name])){
        $name=$_FILES[$file_field_name]["name"];
        $size=$_FILES[$file_field_name]["size"];
        $type_file=explode("/",$_FILES[$file_field_name]["type"]);
        $type=strtoupper(end($type_file));
        $tmp_name=$_FILES[$file_field_name]["tmp_name"];
        //check if user choosed a photo
        if(empty($size)){$errors[]="You Must Choose A Photo ";}
        else{
            if(! in_array($type,$allow_exts)){$errors[]="Invalid File Format Must Be ".implode(",",$allow_exts);}
            if($size>$max_photo_uploading_size){$errors[]="Too Large File the Size Must be under ". $max_photo_uploading_size." Bytes";} //end else} 
    }else{$errors[]="Somthing Went Wrong";}
    return $errors;
//end function
}
$errors=$function->Controle_upload_file("image"); //image attribute is the name attribute in the Input File Field just as name that used for the Post Methode

【讨论】:

    猜你喜欢
    • 2017-08-07
    • 2017-09-04
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    相关资源
    最近更新 更多