简体   繁体   English

我怎样才能用大括号替换<?php ?>在 php 文件中?

[英]How can I replace braces with <?php ?> in php file?

I wanna replace braces with <?php ?> in a file with php extension.我想在带有 php 扩展名的文件中用<?php ?>替换大括号。 I have a class as a library and in this class I have three function like these:我有一个类作为库,在这个类中我有三个这样的函数:

 function replace_left_delimeter($buffer)
{
    return($this->replace_right_delimeter(str_replace("{", "<?php echo $", $buffer)));
}

function replace_right_delimeter($buffer)
{
    return(str_replace("}", "; ?> ", $buffer));
}

function parser($view,$data)
{
    ob_start(array($this,"replace_left_delimeter"));
    include APP_DIR.DS.'view'.DS.$view.'.php';
    ob_end_flush();
}

and I have a view file with php extension like this:我有一个带有 php 扩展名的视图文件,如下所示:

{tmp} tmpstr

in output I save just tmpstr and in source code in browser I get在输出中我只保存 tmpstr 和在浏览器中的源代码中我得到

<?php echo $tmp; ?> 
tmpstr

In include file <?在包含文件<? shown as <!--?显示为<!--? and be comment.并发表评论。 Why?为什么?

What you're trying to do here won't work.你在这里尝试做的事情是行不通的。 The replacements carried out by the output buffering callback occur after PHP code has already been parsed and executed.输出缓冲回调执行的替换发生在 PHP 代码已经被解析和执行之后。 Introducing new PHP code tags at this stage won't cause them to be executed.在此阶段引入新的 PHP 代码标记不会导致它们被执行。

You will need to instead preprocess the PHP source file before evaluating it, eg您需要在评估之前对 PHP 源文件进行预处理,例如

$tp = file_get_contents(APP_DIR.DS.'view'.DS.$view.'.php');
$tp = str_replace("{", "<?php echo \$", $tp);
$tp = str_replace("}", "; ?>", $tp);
eval($tp);

However, I'd strongly recommend using an existing template engine;但是,我强烈建议使用现有的模板引擎; this approach will be inefficient and limited.这种方法将是低效和有限的。 You might want to give Twig a shot, for instance.例如,您可能想尝试一下Twig

do this:做这个:

function parser($view,$data)
{
    $data=array("data"=>$data);
    $template=file_get_contents(APP_DIR.DS.'view'.DS.$view.'.php');
    $replace = array();
    foreach ($data as $key => $value) {
        #if $data is array...
        $replace = array_merge(
            $replace,array("{".$key."}"=>$value)
            );
    }

    $template=strtr($template,$replace);
    echo $template;
}

and ignore other two functions.并忽略其他两个函数。

How does this work:这是如何运作的:

process.php :进程.php

<?php

$contents = file_get_contents('php://stdin');

$contents = preg_replace('/\{([a-zA-Z_][a-zA-Z_0-9]*)\}/', '<?php echo $\1; ?>', $contents);
echo $contents;

bash script : bash脚本

process.php < my_file.php

Note that the above works by doing a one-off search and replace.请注意,上述方法是通过一次性搜索和替换来实现的。 You can easily modify the script if you want to do this on the fly.如果您想即时执行此操作,您可以轻松修改脚本。

Note also, that modifying PHP code from within PHP code is a bad idea.另请注意,从 PHP 代码中修改 PHP 代码是一个坏主意。 Self-modifying code can lead to hard-to-find bugs, and is often associated with malicious software.自修改代码会导致难以发现的错误,并且通常与恶意软件有关。 If you explain what you are trying to achieve - your purpose - you might get a better response.如果你解释你想要达到的目标——你的目的——你可能会得到更好的回应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM