[英]Open the text file in the browser
我使用以下命令在瀏覽器中顯示文本文件:
echo file_get_contents($filename);
但是此命令連續顯示消息行。 我想顯示文章的行,例如一個單獨的文本文件。 您有什么解決方案?
您可以使用nl2br
在字符串中的所有換行符之前插入HTML換行符。
nl2br(字符串$ string [,bool $ is_xhtml = TRUE]):字符串
返回在所有換行符(
\\r\\n
,\\n\\r
,\\n
和\\r
)之前插入<br />
或<br>
字符串。
用法:
$content = file_get_contents($filename);
echo nl2br($content);
僅使用file_get_contents才能在瀏覽器中將文件正確輸出為文本,問題是瀏覽器會將其解釋為html。 在html中,多個空格被“合並”,因此您的文件顯示為連續列表。
您必須在這里:
header('Content-Type:text/plain');
echo file_get_contents($filename);
$text = file_get_contents($filename);
echo "<pre>".htmlEntities($text)."</pre>";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.