php html怎么输出空格,如何使用PHP/HTML保持空格格式?

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 linebreaks 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.

我正在解析文件中的文本並將其存儲在一個字符串中。問題是原始文件中的一些文本包含ASCII藝術和其他我想保存的東西。當我在HTML頁面上打印字符串時,即使它具有相同的格式和所有內容,因為它是在HTML中,所以空格和換行符不會被保留。在HTML中打印文本的最佳方式是什么,就像在原始文本文件中那樣?我想舉個例子,但不幸的是,我無法讓它在這個markdown編輯器中正確顯示:P基本上,我想要關於如何在HTML中顯示ASCII藝術的建議。

4 个解决方案

#1

53

use the

 tag (pre formatted), that will use a mono spaced font (for your art) and keep all the white space

使用

標記(預格式化),它將使用單行間距字體(用於您的藝術)並保留所有空白

 
 

text goes here and here

and here and here Some out here

▄ ▄█▄ █▄ ▄

▄█▀█▓ ▄▓▀▀█▀ ▀▀▀█▓▀▀ ▀▀ ▄█▀█▓▀▀▀▀▀▓▄▀██▀▀

██ ██ ▀██▄▄ ▄█ ▀ ░▒ ░▒ ██ ██ ▄█▄ █▀ ██

█▓▄▀██ ▄ ▀█▌▓█ ▒▓ ▒▓ █▓▄▀██ ▓█ ▀▄ █▓

█▒ █▓ ██▄▓▀ ▀█▄▄█▄▓█ ▓█ █▒ █▓ ▒█ ▓█▄ ▒

▀▒ ▀ ▀ █▀ ▀▒ ▀ █▀ ░

You might have to convert any

你可能要把任何< s轉換成

#2

10

the

 and 
might not be ideal in textarea etc..

和在文本區域可能不理想。

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

當想要保存新線路時,請使用UnkwnTech和Brad Mace所提到的nl2br。

When wanting to preserve spaces use str_replace:

當想要保留空間時,使用str_replace:

str_replace(' ', ' ', $stringVariable);

(大小寫不敏感' ',',',stringVariable美元);

When both use this:

當同時使用:

$result = str_replace(' ', ' ', $stringVariable);

$result = nl2br($result);

#3

6

When you print the data use nl2br() to convert \n and \r\n into

當您打印數據時,使用nl2br()將\n和\r\n轉換為

#4

1

For all those searchng for preserving the text fetched from database , this worked for me , setting CSS as following ,

對於所有保存從數據庫中獲取的文本的searchng,這對我來說是有用的,設置CSS如下,

pre {

white-space: pre-line;

text-align : left;

}

in html :

在html中:

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值