gpt4 book ai didi

php - 将一个巨大的 html 文件转换为 php 变量/echo

转载 作者:行者123 更新时间:2023-11-28 04:14:42 26 4
gpt4 key购买 nike

我有一个巨大的 HTML 文件,我想将其转换为一个变量以供输出。所以,像这样:

<div class="section">
<h3>hey<? echo $anothervar ?></h3>
<input type="submit" id="submit" name="submit" value="Submit" class="button" />
</div>

$myvar;

$myvar="<div class=\"section\">
<h3>hey" . $anothervar . "</h3>";
$myvar.= "<input type=\"submit\" id=\"submit\" name=\"submit\" value=\"Submit\" class=\"button\" />
</div>";

赖夫。除了它是一个巨大的文件,里面装满了 PHP,但我需要将它全部放在一个变量中以传递它然后返回它。有没有一种自动化的方法来做到这一点(希望在 Windows 上使用 NP++ 之类的东西?)或者至少有一种方法可以自动化其中的很大一部分,而不是让我花费数小时和数小时的时间来完成这些工作?

编辑:每个人都在争论我为什么要这样做。这是快速但可能很无聊的回应;我正在为 WordPress 编写一个插件,它接受一个短代码并将其替换为单个变量的输出;如果只是回应(就像在我收到的文件中所做的那样),那么 WP 会将简码输出放在与内容相关的不同位置。所以,是的,您可能不想这样做,但这就是原因 (http://codex.wordpress.org/Shortcode_API#Overview):

When the_content is displayed, the shortcode API will parse any registered shortcodes such as "[my-shortcode]", separate and parse the attributes and content, if any, and pass them the corresponding shortcode handler function. Any string returned (not echoed) by the shortcode handler will be inserted into the post body in place of the shortcode itself.

最佳答案

我不认为这是个好主意。如果只回显单个变量,它看起来很好(尽管它不是 HTML 文件而是 PHP 文件,并且您的示例 <? echo $anothervar> 既不是有效的 HTML 也不是有效的 PHP)。

查看 PHP output buffering如果要将结果存储在变量中:

<?php
ob_start();
?>
<!-- your stuff here -->
<?php
$output = ob_get_clean();
// Do something with output
?>

关于php - 将一个巨大的 html 文件转换为 php 变量/echo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6311963/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com