沃梦达 / 编程问答 / php问题 / 正文

使用 preg_replace 修改 HTML 和 PHP

Modify HTML and PHP with preg_replace(使用 preg_replace 修改 HTML 和 PHP)

本文介绍了使用 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.

推荐答案

不完整的答案.这是我最初使用explode时得到的.它可以工作,但是当向它创建错误的函数添加多个值时.它可以处理一两个值.不多,而且对我来说太复杂了.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 preg_replace 修改 HTML 和 PHP

基础教程推荐