dongou4052 2011-06-10 20:53
浏览 73
已采纳

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

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.

  • 写回答

5条回答 默认 最新

  • dowbwrr3590709 2011-06-10 20:59
    关注

    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
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题