[英]Get HTML content and show this as plain text
I get content of a html file:我得到一个 html 文件的内容:
ob_start();
include ("myFile.html");
$html = ob_get_contents();
Now I would like to send $html as plain text via mail to show the code.现在我想通过邮件将 $html 作为纯文本发送以显示代码。 I tried
"<pre>".$html."</pre>"
and "<code>".$html."</code>"
我试过
"<pre>".$html."</pre>"
和"<code>".$html."</code>"
But I get not the plain code.但我没有得到纯代码。 Can you help me?
你能帮助我吗?
Try something like:尝试类似:
<?php
$filePath = __DIR__ . '/myFile.html';
$content = file_get_contents($filePath);
// Escape.
$content = htmlspecialchars($content);
// Makes lines visible.
$content = preg_replace('/\n/', '<br>' . PHP_EOL, $content);
echo $content;
Note that I use single-quotes to improve performance slightly, but when using double-quote, the pattern would be
"/\\n/"
(to escape Back-slash, but should work even without escaping).请注意,我使用单引号来稍微提高性能,但是当使用双引号时,模式将是
"/\\n/"
(用于转义反斜杠,但即使没有转义也应该可以工作)。
I know Top-Master answered this question, but this is an alternative method, that still uses include
.我知道 Top-Master 回答了这个问题,但这是另一种方法,仍然使用
include
。 I didn't know enough about PHP to know about the fancy methods, that are probably much more secure.我对 PHP 的了解不够多,无法了解可能更安全的花哨方法。
The <pre>
tag doesn't guarantee that the contents will be displayed verbatim. <pre>
标签不保证内容会逐字显示。 For example <pre><a>Hello</a></pre>
will show up as Hello , not <a>Hello</a>.例如
<pre><a>Hello</a></pre>
将显示为Hello ,而不是 <a>Hello</a>。 To solve this you can use <为了解决这个问题,您可以使用 < (less than sign) and &rt;
(小于号)和 &rt; (greater than sign).
(大于符号)。 If you need and actual ampersand ("&"), you can use &.
如果您需要和实际的 & 符号 ("&"),您可以使用 &。
This modified version should escape <, >, and &.这个修改后的版本应该转义 <、> 和 &。 Do not rely on this in terms of security.
在安全性方面不要依赖于此。
ob_start();
include ("myFile.html");
$html = ob_get_contents();
$html = str_replace("&", "&". $html);
$html = str_replace("<", "<". $html);
$html = str_replace(">", ">", $html);
$html = str_replace("\n", "<br/>");
To show the HTML code in the browser just take readfile() and set the header for Text/Plain.要在浏览器中显示 HTML 代码,只需使用readfile()并将 header 设置为 Text/Plain。
<?php
header('Content-Type: text/plain; charset=UTF-8');
readfile("myFile.html");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.