开发者

Modify HTML and PHP with preg_replace [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜