将一个巨大的 html 文件转换为 php 变量/echo
我有一个巨大的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为这不是一个好主意。如果仅回显单个变量,则看起来相当不错(尽管它不是 HTML 文件,而是 PHP 文件,并且您的示例
既不是有效的 HTML,也不是有效的 PHP)。
如果您想将结果存储在变量中,请查看 PHP 输出缓冲:
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:
你也可以用这个...
You could also use this ...
您可以使用这个:
HTML 到 PHP 转换器
You can use this:
HTML To PHP Converter
请尝试这个代码
please try this code