Modify HTML and PHP with preg_replace [closed]

2020-03-31 10:16发布

I have this HTML / PHP content (as a string):

<html>
    <?php my_class->my_function('test.php', 'value1', 'value2');
    <?php my_class->my_function('test2.php', 3);
</html>

What my_class->my_function does is including content depending on the values sent.

It could look something like this

function my_function($file, $value1, $value2)
{
    include $file . $value1 . $value2;
}

The result of my_function should be outputted, instead of the function call.

The result should be

<html>
    <?php
        /* Content from file test.php */
        echo 'This file is test.php, with value1 and value2';
    ?>
    <?php
        /* Content from file test2.php */
        echo 'This file is test2.php, with value 3';
    ?>
</html>

I guess I need some kind of preg_replace in combination with eval, include or get_file_contents.

1条回答
劳资没心,怎么记你
2楼-- · 2020-03-31 10:40

Uncomplete answer. This is what I got at first when using explode. It works BUT when adding multiple values to the function it creates bugs. One or two values it can handle. Not more and it's too complex for me to handle.

function include_layout2($part, $file = '/index.php')
{

    $url = get_layout2($part, $file);
    if(file_exists($url))
    {
        return get_include_contents( $url );
    }
}


function get_layout2($part, $file = '/index.php')
{
    $layout_theme = 'default';
    $addmod_layout = get_option('addmod_layout');
    $layout = $addmod_layout['styles'][$layout_theme][$part]['layout'];
    $layout = (!empty($layout)) ? $layout : 'default';
    $url = TEMPLATEPATH . '/styles/' . $part . '/' . $layout . $file;
    if(file_exists($url))
    {
        return $url;
    }
}

function generate_content($layout = 'index')
{   
    $content = include_layout2($layout);
    $content_a = explode("<?php addmod_include('", $content);

    $save_for_later = $content;
    $counter = 0;
    while(count($content_a) > 1)
    {
        $content_a = explode("<?php addmod_include('", $save_for_later);
        $output = '';
        $save_for_later = '';
        for($i = 0; $i < sizeof($content_a); $i++)
        {
            $content_b = explode("'); ?>", $content_a[$i], 2);
            if(isset($content_b[0]) && !empty($content_b[0]))
            {
                $include_test = include_layout2($content_b[0]);
                if(isset($include_test))
                {
                    $content_b[0] = include_layout2($content_b[0]);
                }
                else
                {
                    $new_layout = explode("addmod_include('", $content_b[0]);
                    $new_file = explode("addmod_include(', '", $content_b[0]);

                    if(isset($new_layout[0]) && isset($new_file[1]))
                    {
                        $content_b[0] = include_layout2($new_layout[0], $new_file[1]);
                    }
                    else
                    {
                        $content_b[0] .=  "'); ?>";
                    }
                }
            }
            $content = implode($content_b);
            $output .= $content;
            $save_for_later .= $content;
        }

        $counter++;
    }
    return $output;
}
echo generate_content();

Don't try to run the code because it's a little dependent of some other stuff.

I don't know if that makes it more clear what I need to do?

查看更多
登录 后发表回答