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

发布于 2024-11-15 01:48:46 字数 1102 浏览 3 评论 0原文

我有一个巨大的 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>";

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

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

当显示 the_content 时, 短代码 API 将解析任何 注册的短代码,例如 “[my-shortcode]”,分离并解析 属性和内容(如果有) 并传递给他们相应的 短代码处理函数。任意字符串 由短代码返回(不回显) 处理程序将被插入到帖子中 正文代替短代码本身。

I have an enormous file of HTML that I'd like to convert to one variable for output. So, something like:

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

to

$myvar;

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

ryv. except its a HUGE file, with PHP peppered in there, but I need to get it all in one variable to pass it and then return it. Is there an automated way to do this (hopefully on windows with something like NP++?) or at least a way to automate a good portion of this rather than taking me hours and hours of going through the lines?

EDIT: Everyone is debating why I want to do that. Here's the quick and perhaps boring response; I'm writing a plugin for WordPress that takes a shortcode and replaces it with the output of a single variable; if one just echoes (as it is done in the file I was given) then WP puts the shortcode output in a different place relative to the content. So, yes, you may not want to do this but that's the reason (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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

地狱即天堂 2024-11-22 01:48:46

我认为这不是一个好主意。如果仅回显单个变量,则看起来相当不错(尽管它不是 HTML 文件,而是 PHP 文件,并且您的示例 既不是有效的 HTML,也不是有效的 PHP)。

如果您想将结果存储在变量中,请查看 PHP 输出缓冲

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

I don't think that is a good idea. If only single variables are echoed it looks quite fine (although it's not an HTML file but a PHP file and your example <? echo $anothervar> is neither valid HTML nor PHP).

Look at PHPs output buffering if you want to store the result in a variable:

<?php
    ob_start();
?>
<!-- your stuff here -->
<?php
    $output = ob_get_clean();
    // Do something with output
?>
乞讨 2024-11-22 01:48:46

你也可以用这个...

$your_var = <<<EOF

a bunch of html goes here

EOF;

You could also use this ...

$your_var = <<<EOF

a bunch of html goes here

EOF;
浪漫之都 2024-11-22 01:48:46
ob_start();
include("myfile.html");
$var = ob_get_clean();
ob_start();
include("myfile.html");
$var = ob_get_clean();
初雪 2024-11-22 01:48:46

您可以使用这个:

HTML 到 PHP 转换器

You can use this:

HTML To PHP Converter

彩扇题诗 2024-11-22 01:48:46

请尝试这个代码

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

     ?>

please try this code

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

     ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文