preg_replace in multidimensional array

Viewed 35

I have a multidimensional array which is:

[
    'lesson-add-cours',
    [
        'form',
        [
            ['element-txt',['h5','Ajouter un cours',null,null],null,null],
            ['element-sbj',
                [
                ['subject-itm',
                    [
                        ['subtext','Nom <span>*</span>',null,null],
                        ['input','','text',['class'=>'_input_scd','placeholder'=>'Nom affiché','required'=>'true']],
                    ],
                    null,null],
                ['subject-itm',
                    [
                        ['subtext','Durée',null,null],
                        ['input','','number',['class'=>'_input_scd','placeholder'=>'10 mins']],
                    ],null,null],
                ['subject-itm',
                    [
                        ['subtext','Niveau du cours <span>*</span>',null,null],
                        ['input','','hidden',['class'=>'_input_scd','placeholder'=>'10 mins','required'=>'true']],
                        ['div',
                            [
                                ['button-img','<img src="ntf.png" alt=""><h6>0</h6><p>Apprenant</p>',null,null],
                                ['button-img','<img src="ntf.png" alt=""><h6>1</h6><p>Formateur</p>',null,null],
                                ['button-img','<img src="ntf.png" alt=""><h6>2</h6><p>Instructeur</p>',null,null],
                            ],null,null
                        ],
                    ],null,null],
                ],null,null],
            ['element-btn',[
                ['input','Retour','button',['class'=>'_input-scd']],
                ['input','Dupliquer','button',['class'=>'_input-scd']],
                ['input','Ajouter','submit',['class'=>'_input-pri']],
            ],null,null],
        ],'action',null,
    ],null,null,
];

I want this array to be used as html creator. I have also a function that creates html elements by sending it 4 arguments (which are in the array).

My function to transform array is:

public static function create($type, $content = null, $parameter = null, $attribute = null)
    {
        $_temp_parameter = '';
        $_temp_attribute = '';

        $_temp_return = '';

        if(!is_null($parameter))
        {
            if(is_array($parameter))
            {
                $_temp_parameter = implode(' ',$parameter);
            }
            else
            {
                $_temp_parameter = $parameter;
            }
        }
        if(!is_null($attribute))
        {
            for($i=0;$i<sizeof($attribute);$i++)
            {
                $_temp_attribute .= array_keys($attribute)[$i].'="'.$attribute[array_keys($attribute)[$i]].'"';
            }
            $_temp_attribute = $_temp_attribute;
        }

        if(array_key_exists($type,self::$typeMap))
        {
            if(is_array($content))
            {
                $_temp_return = sprintf(self::$typeMap[$type],'',$_temp_parameter,$_temp_attribute);
                return vnsprintf($_temp_return,$content);
            }
            return sprintf(self::$typeMap[$type],$content,$_temp_parameter,$_temp_attribute);
        }
    }
    public static function replaceText($text)
    {
        return str_replace('"','\"',$text);
    }

I tried by preg_replace replacing my array into text, but it isn't working. Here is my try:

    $test =json_encode($array);
    $abc = preg_replace('/\[(.*),(.*),(.*),(.*)\]/','createContent::create($1,$2,$3,$4)',$test);

I want to have something like that but automated:

createContent::create('lesson-add-cours',createContent::create('form',createContent::create('element-txt',createContent::create('h5','Ajouter un cours',null,null),null,null).createContent::create('element-sbj',etc etc),'action','null'));
0 Answers
Related