使用PHP将制表符转换为用于HTML显示的空格? [英] Using PHP to convert tabs to spaces for HTML display?
问题描述
我需要在网页中显示包含双空格tab'd数据列的纯文本文件。
我所做的是使用PHP阅读文本文件并在< pre>
标签之间打印,以便使用等宽字体:
<预>
<?php
$ fn =data.txt;
$ fi = fopen($ fn,r);
$ fc = fread($ fi,filesize($ fn)); //打开并读取文本文件
fclose($ fi);
$ fc = str_replace(\t,,$ fc); //用两个空格替换标签
print($ fc); //在PRE标签之间打印数据
?>
< / pre>
它几乎可以工作,但这些标签很麻烦。用两个空格替换标签是很简单的,但是非空白字符被推送而不是吸收到标签中。 True标签吸收 n-1
非空白字符(其中 n
是每个标签的空格数) p>
例如,下面的表格应该显示为:
| | 43 | 43 | 7 | | |
| 12 | 128 | 128 | 128 | | 53 |
| 3 | 3 | 3 | 3 | | |
| | | 21 | 21 | 39 | |
然而,通过盲目地用两个空格替换所有 :
| | 43 | 43 | 7 | | |
| 12 | 128 | 128 | 128 | | 53 |
| 3 | 3 | 3 | 3 | | |
| | | 21 | 21 | 39 | |
我试图找出一个(相当简单)的方法来将标签转换为空格,而会计对于不占用完整 n
空格的选项卡。
我前段时间写过这个函数,可能会有帮助:
函数tab2space($ line,$ tab = 4,$ (($ t = mb_strpos($ line,\ t))!== FALSE){
$ preTab = $ t?mb_substr($ line,0, $ T): '';
$ line = $ preTab。 str_repeat($ nbsp?chr(7):'',$ tab-(mb_strlen($ preTab)%$ tab))。 mb_substr($ line,$ t + 1);
}
return $ nbsp?str_replace($ nbsp?chr(7):'','& nbsp;',$ line):$ line;
$ b 这是为了处理多字节字符串,如果你只有数字,你可以摆脱 mb _
,它会加快这个功能。
请注意,这是为了使用一行,因此您需要一次性逐行处理 fgets
而不是整个文件。
I need to display a plain-text file that contains two-space tab’d columns of data in a web page.
What I did was to use PHP to read the text file and print it out between <pre>
tags to use a monospace font as so:
<pre>
<?php
$fn="data.txt";
$fi=fopen($fn, "r");
$fc=fread($fi, filesize($fn)); //open and read text file
fclose($fi);
$fc=str_replace("\t", " ", $fc); //replace tabs with two spaces
print($fc); //print data between PRE tags
?>
</pre>
It almost works, but the tabs are being troublesome. It is trivial to replace the tabs with two spaces, but then non-whitespace characters are pushed over instead of absorbed into the tabs. True tabs absorb n-1
non-whitespace characters (where n
is the number of spaces per tab).
For example, the following table should be displayed as so:
| | 43| 43| 7| | |
| 12|128|128|128| | 53|
| 3| 3| 3| 3| | |
| | | 21| 21| 39| |
However by blindly replacing all tabs with two-spaces, we get this:
| | 43| 43| 7| | |
| 12|128|128|128| | 53|
| 3| 3| 3| 3| | |
| | | 21| 21| 39| |
I’m trying to figure out a (reasonably easy) way to convert the tabs to spaces while accounting for tabs that don’t take up the full n
spaces.
解决方案 I have written this function some time ago, might be helpful:
function tab2space($line, $tab = 4, $nbsp = FALSE) {
while (($t = mb_strpos($line,"\t")) !== FALSE) {
$preTab = $t?mb_substr($line, 0, $t):'';
$line = $preTab . str_repeat($nbsp?chr(7):' ', $tab-(mb_strlen($preTab)%$tab)) . mb_substr($line, $t+1);
}
return $nbsp?str_replace($nbsp?chr(7):' ', ' ', $line):$line;
}
It was meant to deal with multibyte strings, if you have only numbers, you can get rid of mb_
, it will speed up this function.
[+] Note that this is meant to work with one line, so you will need to process line by line with fgets
instead of whole file at once.
这篇关于使用PHP将制表符转换为用于HTML显示的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!