Modify HTML and PHP with preg_replace [closed]

风格不统一 提交于 2019-12-01 12:07:21

问题


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:


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?



来源:https://stackoverflow.com/questions/4724564/modify-html-and-php-with-preg-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!