English 中文(简体)
如何使用PHP/HTML保持空白格式?
原标题:
  • 时间:2008-08-31 05:55:12
  •  标签:

I m parsing text from a file and storing it in a string. The problem is that some of the text in the original files contains ASCII art and whatnot that I would like to preserve. When I print out the string on the HTML page, even if it does have the same formatting and everything since it is in HTML, the spacing and line breaks are not preserved. What is the best way to print out the text in HTML exactly as it was in the original text file?
I would like to give an example, but unfortunately, I was not able to get it to display correctly in this markdown editor :P
Basically, I would like suggestions on how to display ASCII art in HTML.

最佳回答

使用<;预>;标签(预格式化),将使用单间距字体(用于您的艺术)并保留所有空白

<pre>
text goes here and here 
             and here and here            Some out here
     ▄             ▄█▄ █▄       ▄
 ▄█▀█▓ ▄▓▀▀█▀ ▀▀▀█▓▀▀ ▀▀    ▄█▀█▓▀▀▀▀▀▓▄▀██▀▀
██  ██ ▀██▄▄ ▄█  ▀ ░▒ ░▒   ██  ██ ▄█▄ █▀ ██
█▓▄▀██  ▄ ▀█▌▓█    ▒▓ ▒▓   █▓▄▀██ ▓█ ▀▄  █▓
█▒  █▓ ██▄▓▀ ▀█▄▄█▄▓█ ▓█   █▒  █▓ ▒█  ▓█▄ ▒
    ▀▒           ▀  ▀ █▀       ▀▒  ▀  █▀  ░

</pre>  

您可能需要转换任何<;s到&;书信电报;s

问题回答

<;预></预>在文本区域等中可能不理想。。

When wanting to preserve new line - and use nl2br as mentioned by UnkwnTech and Brad Mace.

想要保留空间时,请使用str_replace

str_replace(,&;nbsp;,$stringVariable)

当两者都使用此选项时:

$result = str_replace(   ,  &nbsp; , $stringVariable);
$result = nl2br($result);

When you print the data use nl2br() to convert and into <br>

对于所有那些试图保存从数据库中提取的文本的搜索,这对我来说很有效,设置CSS如下,

pre {
     white-space: pre-line;
     text-align : left;
  }

在html中:

<pre >
     <?php echo htmlentities($yourText ) ; ?>
</pre>

just echo the necessary special characters (s, , or ) along with your string in your PHP code.

<?php 

echo ("hello world 
")

?>




相关问题