简体   繁体   English

如何解析.ini文件并在php中生成代码

[英]How to parse .ini file and generate code in php

I'm new to .ini and php. 我是.ini和php的新手。 I'm using PFBC to generate form fields. 我正在使用PFBC生成表单字段。 This is the code sample to generate one form field: 这是生成一个表单字段的代码示例:

$form->addElement(new Element\T_COMPANY("", "company", array(
"required" => 1,
"placeholder" => "*Bedrijf:",
)));

I need to read an .ini file and based on what is in that .ini to generate one or more lines of code to make the form field. 我需要读取一个.ini文件,并基于该.ini文件中的内容来生成一行或多行代码来构成表单字段。 The .ini file will contain some thing like : .ini文件将包含以下内容:

fields[company] = T_COMPANY,"","company",1,"*Company"

where T_COMPANY is the identifier to generate this block of code $form->addElement(new Element\\T_COMPANY( next "" is the identifier for "" in the php code next "company" is the identifier for "company" in the php code next 1 identifier for "required" => 1, in the php and the last one "*Bedrijf" identifier for "placeholder" => "*Bedrijf:", Any idea on how to do this ? 其中T_COMPANY是生成代码的该块标识符$form->addElement(new Element\\T_COMPANY(下一个""是标识符""在php代码下一个"company"是标识符"company"在php代码在PHP中,下一个1标识符为"required" => 1,最后一个"*Bedrijf"标识符为"placeholder" => "*Bedrijf:",有关如何执行此操作的任何想法?

I tried to use foreach to construct the syntax but is not working 我尝试使用foreach构造语法,但无法正常工作

foreach ($formConfig as $elementConfig) {
    switch (strtolower($elementConfig['type'])) {
    case 'text':
        $className = 'Element\T_COMPANY';
            break;
    default:
      throw new Exception();
      break;
  }
  $formElement = new $className("", "Required", array( 
    "required" => 1,
    "placeholder" => "*Bedrijf:",
    ));
  $form->addElement($formElement);
}
$form = new Form($formConfig);

This is what helped me 这就是帮助我的原因

foreach ($ini_array['FORM_SETTINGS'] as $type => $fieldsData) {

        if ($fieldsData['type'] == 'HTML'){
                $className = "PFBC\\Element\\{$fieldsData['type']}";
                $form->addElement(new $className($fieldsData['string']
                ));
            continue;}

        if ($fieldsData['required'] == true){
            $className = "PFBC\\Element\\{$fieldsData['type']}";
            $form->addElement(new $className($fieldsData['label'], $fieldsData['id'], array(
            'required' => $fieldsData['required'],
            'placeholder' => $fieldsData['placeholder'],
                )
            ));
        }

        if ($fieldsData['required'] == false){
            $className = "PFBC\\Element\\{$fieldsData['type']}";
            $form->addElement(new $className($fieldsData['label'], $fieldsData['id'], array(
            'placeholder' => $fieldsData['placeholder'],
                )
            ));
        }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM