使用preg_replace修改HTML和PHP [英] Modify HTML and PHP with preg_replace

查看:134
本文介绍了使用preg_replace修改HTML和PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个HTML / PHP内容(作为字符串):

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

my_class-> my_function的作用包括内容,具体取决于发送的值。

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

它可能看起来像这样

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

应该输出my_function的结果,而不是函数调用。

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

结果应为

<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>

我想我需要某种preg_replace与eval,include或get_file_contents结合使用。

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?

这篇关于使用preg_replace修改HTML和PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆